C
C#15mo 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
Azrael15mo 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 🌊💃15mo 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_F15mo ago
@Tycho did you try asking Bing AI to do that for you?
Tycho
TychoOP15mo 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
Azrael15mo ago
This is what I based my thing on. Try to divide methods into smaller methods. Makes it easier to read.
Hazel 🌊💃
Hazel 🌊💃15mo ago
Is it a native type or something the OP would have to create on their own?
Azrael
Azrael15mo ago
Native to Unity I think. It says LayerMask, so I just assumed that it was a common type.
Tycho
TychoOP15mo ago
Yeah I was planning on refactoring when the entire game was done Yes, LayerMask is a Unity component
Azrael
Azrael15mo ago
Okay.
Accord
Accord15mo 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