- Home /
Optimizing Code
HI, so I've been learning c# for the past weeks and it's been a nice journey. I have this Code that I've made using tutorials and myself and I'm pretty sure is not optimized at all. I've realized that following tutorials from A to D only makes me start copy pasting code as I reach point E so I decided to try to get as much as I could do myself searching on the internet and solving problems myself instead of follwing thos tutorials, however there is a limit on what I can do and I'm pretty sure I've made the code not optimized at all since my problem solving is not good, so I'd like to ask for help in how to optimize this.
public class Weapon : MonoBehaviour {
[Header("ShotPoint")]
public Transform horizontalShotPoint;
public Transform VerticalShotPoint;
[Header("CanShoot")]
public bool xCanShoot = true;
public bool yCanShoot = true;
[Header("Recoil")]
public float horizontalRecoil;
public float verticalRecoil;
[Header("Projectile")]
public GameObject Projectile;
public float xProjectileSpeed;
public float yProjectileSpeed;
[Header("Cooldown")]
public float horizontalStartTimeBtwShots;
public float VerticalStartTimeBtwShots;
Rigidbody2D rb;
Player player;
private float timeBtwShots;
void Start() {
rb = GetComponent<Rigidbody2D>();
player = GetComponent<Player>();
}
void Update() {
if(timeBtwShots <= 0) {
if(Input.GetKey(KeyCode.DownArrow)) {
xCanShoot = false;
} else {
xCanShoot = true;
}
if(Input.GetButtonDown("Fire1") && xCanShoot) {
horizontalShoot();
timeBtwShots = horizontalStartTimeBtwShots;
} else if (Input.GetKey(KeyCode.DownArrow) && Input.GetButtonDown("Fire1") && yCanShoot){
CooldownBar.isntance.UseStamina(100);
player.anim.SetBool("ShootDown", true);
VerticalShoot();
timeBtwShots = VerticalStartTimeBtwShots;
}
} else {
timeBtwShots -= Time.deltaTime;
}
}
void horizontalShoot() {
if(player.facingRight == true) {
rb.AddForce(new Vector2(-horizontalRecoil, 0));
GameObject go = (GameObject)Instantiate(Projectile, horizontalShotPoint.position, horizontalShotPoint.rotation);
go.GetComponent<Projectile>().xSpeed = xProjectileSpeed;
go.GetComponent<Projectile>().ySpeed = 0f;
} else {
rb.AddForce(new Vector2(horizontalRecoil, 0));
GameObject go = (GameObject)Instantiate(Projectile, horizontalShotPoint.position, horizontalShotPoint.rotation);
go.GetComponent<Projectile>().xSpeed = -xProjectileSpeed;
go.GetComponent<Projectile>().ySpeed = 0f;
}
}
void VerticalShoot() {
rb.AddForce(new Vector2(0, verticalRecoil));
GameObject go = (GameObject)Instantiate(Projectile, VerticalShotPoint.position, VerticalShotPoint.rotation);
go.GetComponent<Projectile>().ySpeed = -yProjectileSpeed;
go.GetComponent<Projectile>().xSpeed = 0f;
}
}
public class Projectile : MonoBehaviour {
[HideInInspector] public float xSpeed;
[HideInInspector] public float ySpeed;
public float lifeTime;
public float distance;
public LayerMask whatIsSolid;
public GameObject destroyEffect;
void Start() {
Invoke("DestroyProjectile", lifeTime);
}
void Update() {
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
if(hitInfo.collider != null) {
if(hitInfo.collider.CompareTag("Ground")) {
DestroyProjectile();
}
}
Vector2 position = transform.position;
position.x += xSpeed;
position.y += ySpeed;
transform.position = position;
}
void DestroyProjectile() {
Instantiate(destroyEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
these are a bullet code and a weapon code.
Answer by Llama_w_2Ls · Dec 27, 2020 at 10:03 AM
I believe your code is quite optimized in the Updates (no unnecessary object references or hefty processes), however, in your horizontalShoot()
method, and VerticalShoot()
method, you're wasting a few clock cycles 'getting' components and writing more code than you need to, which does add up, but not significantly. Here's what I would do for your horizontalShoot() method instead:
void horizontalShoot()
{
GameObject go = (GameObject)Instantiate(Projectile, horizontalShotPoint.position, horizontalShotPoint.rotation);
Projectile projectileScript = go.GetComponent<Projectile>();
if (player.facingRight == true)
{
rb.AddForce(new Vector2(-horizontalRecoil, 0));
projectileScript.xSpeed = xProjectileSpeed;
projectileScript.ySpeed = 0f;
}
else
{
rb.AddForce(new Vector2(horizontalRecoil, 0));
projectileScript.xSpeed = -xProjectileSpeed;
projectileScript.ySpeed = 0f;
}
}
ohhh! thanks very much for your answer. I was worried about those 2 function and I'm glad the biggest problem is about instantiating the bullet twice. I though aswell about referencing bullet twice for the velocity but I guess thats not a problem.
It would be better if you did the same for VerticalShoot() as well.
DId the same to verticalShoot(); and moved stuff from update function to here as well void Update() { if(timeBtwShots <= 0) { if(Input.GetKey(KeyCode.DownArrow)) { xCanShoot = false; } else { xCanShoot = true; } if(Input.GetButtonDown("Fire1") && xCanShoot) { horizontalShoot(); timeBtwShots = horizontalStartTimeBtwShots; } else if (Input.GetKey(KeyCode.DownArrow) && Input.GetButtonDown("Fire1") && yCanShoot){ VerticalShoot(); timeBtwShots = VerticalStartTimeBtwShots; } } else { timeBtwShots -= Time.deltaTime; } }
//void horizontalShoot() {
void VerticalShoot() {
GameObject go = (GameObject)Instantiate(Projectile, VerticalShotPoint.position, VerticalShotPoint.rotation);
Projectile projectileScript = go.GetComponent<Projectile>();
CooldownBar.isntance.UseSta$$anonymous$$a(100);
player.anim.SetBool("ShootDown", true);
rb.AddForce(new Vector2(0, verticalRecoil));
projectileScript.ySpeed = -yProjectileSpeed;
projectileScript.xSpeed = 0f;
}
}
but I gues I can reference to the script bullet like that, realy cool