C
C#10mo ago
Tycho

❔ Trying to shorten this code.

The code works well, but it's a bit dual coded so I've been trying to shorten it. I can't find a good fix, though.
void EnemyRaycastCheck() {
streakScriptReference.HandleStreakBar();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) {
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, enemyLayerMask2D)) {
adrenaline++;
adrenalineScriptReference.SetAdrenaline(adrenaline);
adrenalineScriptReference.ImproveAdrenalineBar();
hitEnemy2D = hit.transform.GetComponent<Enemy2D>();
hitEnemy2D.ReduceHealth();
if (hitEnemy2D.GetHealth() == 0) {
Destroy(hitEnemy2D.gameObject);
}
streakScriptReference.ScorePlus();
} else if (Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask3D)) {
adrenalineScriptReference.DeclineAdrenalineBar();
streakScriptReference.ResetStreak();
}
}
if (Input.GetMouseButtonDown(1)) {
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, enemyLayerMask3D)) {
adrenaline++;
adrenalineScriptReference.SetAdrenaline(adrenaline);
adrenalineScriptReference.ImproveAdrenalineBar();

hitEnemy3D = hit.transform.GetComponent<Enemy3D>();
hitEnemy3D.ReduceHealth();
if (hitEnemy3D.GetHealth() == 0) {
Destroy(hitEnemy3D.gameObject);
}
streakScriptReference.ScorePlus();
} else if (Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask2D)) {
adrenalineScriptReference.DeclineAdrenalineBar();
}
}
}
void EnemyRaycastCheck() {
streakScriptReference.HandleStreakBar();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) {
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, enemyLayerMask2D)) {
adrenaline++;
adrenalineScriptReference.SetAdrenaline(adrenaline);
adrenalineScriptReference.ImproveAdrenalineBar();
hitEnemy2D = hit.transform.GetComponent<Enemy2D>();
hitEnemy2D.ReduceHealth();
if (hitEnemy2D.GetHealth() == 0) {
Destroy(hitEnemy2D.gameObject);
}
streakScriptReference.ScorePlus();
} else if (Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask3D)) {
adrenalineScriptReference.DeclineAdrenalineBar();
streakScriptReference.ResetStreak();
}
}
if (Input.GetMouseButtonDown(1)) {
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, enemyLayerMask3D)) {
adrenaline++;
adrenalineScriptReference.SetAdrenaline(adrenaline);
adrenalineScriptReference.ImproveAdrenalineBar();

hitEnemy3D = hit.transform.GetComponent<Enemy3D>();
hitEnemy3D.ReduceHealth();
if (hitEnemy3D.GetHealth() == 0) {
Destroy(hitEnemy3D.gameObject);
}
streakScriptReference.ScorePlus();
} else if (Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask2D)) {
adrenalineScriptReference.DeclineAdrenalineBar();
}
}
}
10 Replies
Azrael
Azrael10mo ago
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
Then create a LayerMask object and determine whether or not it's 3D or 2D. Then you'd have to do some logic based on if it's 3D or 2D.
if (enemyLayerMask == enemyLayerMask2D)
if (enemyLayerMask == enemyLayerMask2D)
And so on.
Hazel 🌊💃
Hazel 🌊💃10mo ago
Is there a common type betwen the 2D and 3D masks? I'm not fluent in Unity But there's quite a bit that can be done to reduce redundancies here. The only issue I have mentally is that I don't know if the raycast logic can be simplified to a single method through a common type
bool IsHit(Ray ray, out RaycastHit hit) {
bool mouseDown = Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1);
if (mouseDown)
{
bool is3d = ...;
return is3d
? Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask3D)
: Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask2D);
}

return false;
}
bool IsHit(Ray ray, out RaycastHit hit) {
bool mouseDown = Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1);
if (mouseDown)
{
bool is3d = ...;
return is3d
? Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask3D)
: Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask2D);
}

return false;
}
Starting with something like that There other simplifications:
void IncreaseAdrenaline() {
adrenaline++;
adrenalineScriptReference.SetAdrenaline(adrenaline);
adrenalineScriptReference.ImproveAdrenalineBar();
}

void DecreaseAdrenaline(bool resetStreak = true) {
adrenalineScriptReference.DeclineAdrenalineBar();

if (resetStreak)
streakScriptReference.ResetStreak();
}

void HitEnemy(RaycastHit hit) {
target = hit.transform.GetComponent<Enemy>();
target.ReduceHealth();
if (target.GetHealth() == 0)
Destroy(target.gameObject);
}
void IncreaseAdrenaline() {
adrenaline++;
adrenalineScriptReference.SetAdrenaline(adrenaline);
adrenalineScriptReference.ImproveAdrenalineBar();
}

void DecreaseAdrenaline(bool resetStreak = true) {
adrenalineScriptReference.DeclineAdrenalineBar();

if (resetStreak)
streakScriptReference.ResetStreak();
}

void HitEnemy(RaycastHit hit) {
target = hit.transform.GetComponent<Enemy>();
target.ReduceHealth();
if (target.GetHealth() == 0)
Destroy(target.gameObject);
}
These aren't exact Rather examples you can guide yourself with 🙂
David_F
David_F10mo ago
@Tycho did you try asking Bing AI to do that for you?
Tycho
Tycho10mo ago
I'm basically making a game that includes both 2D and 3D enemies, and then you have a 2D and 3D gun to shoot the respective enemy If that makes sense
Azrael
Azrael10mo ago
This is what I based my thing on. Try to divide methods into smaller methods. Makes it easier to read.
Hazel 🌊💃
Hazel 🌊💃10mo ago
Is it a native type or something the OP would have to create on their own?
Azrael
Azrael10mo ago
Native to Unity I think. It says LayerMask, so I just assumed that it was a common type.
Tycho
Tycho10mo ago
Yeah I was planning on refactoring when the entire game was done Yes, LayerMask is a Unity component
Azrael
Azrael10mo ago
Okay.
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ CEFSharp is not working in WPF Application with DEVExpress.I am working on a mid-sized application that embed the website application in windows and shell view❔ OSS/Examples for JSON Deserialization?I'm working on a mid-sized application that does a lot of json deserialization from third party API ❔ My discord bot Does work in DMs but serversToday i wrote a simple C# bot in .NET framework. It Works well in dms but it does not answer my comm❔ Repository patternCreating a tutorial App and I have a EF Core DbContext injected in a a generic Repostitory<T> class.✅ Using services.PostConfigure<ApiBehaviorOptions> to log Model Validation errors in dotnet6?Is this a safe implementation to log Model Validation errors in dotnet6? ``` public static IServic❔ JavaScript Code for newbie pls help...There are data on the growth of N class students. The growth of boys is conditionally given by negat❔ EFC Is not loading collection properlyI have the following 2 models in my database, EFC does not seem to be loading it at all for some rea✅ .NET Core N-Tier API Project question about repository pattern.Hi, I am creating a web api project using n-tier architecture, I have API, Entities, DAL(Data access❔ ✅ Why does this block the UI thread?```csharp public async void FindWindowHandle(HandleWindowFind handleWindowFind) { if (!started) ❔ Can't create Singleton terminal for dll output debuggingI need to create a CLI to output some debugging information from a DLL, but I am having some issues