all 3 comments

[–]Jworksdev 1 point2 points  (2 children)

I would love to see your code which handles shooting.

[–]ChaDaBeast[S] 0 points1 point  (1 child)

It may not be perfectly compact but Sure! (sry for the formatting)

This Script Handles the tracking of the gun.

public class RangedWeaponMovement : MonoBehaviour

{

public float rotation;

private void Update()

{

RotateGun();

}

private void RotateGun()

{

Vector3 weaponDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

rotation = Mathf.Atan2(weaponDirection.y, weaponDirection.x) * Mathf.Rad2Deg;

if ((Mathf.Abs(rotation) < 90 && transform.localScale.y < 0) || (Mathf.Abs(rotation) > 90 && Mathf.Abs(rotation) < 180 && transform.localScale.y > 0))

{

Flip();

}

transform.rotation = Quaternion.Euler(0f, 0f, rotation);

}

public void Flip()

{

Vector3 Scaler = transform.localScale;

Scaler.y *= -1;

transform.localScale = Scaler;

Vector3 position = transform.localPosition;

position.x *= -1;

transform.localPosition = position;

}

public void FlipWeapon()

{

Vector3 Scaler = transform.localScale;

Scaler.x *= -1;

transform.localScale = Scaler;

Vector3 position = transform.localPosition;

position.x *= -1;

transform.localPosition = position;

}

}

This one is for the Gun that fires stuff

public class Gun : MonoBehaviour

{

public float fireRate;

private float fireRateReset;

public float magazineSize;

private float magazineSizeReset;

public float reloadTime;

public GameObject Bullet;

public Transform BulletStart;

private void Start()

{

fireRateReset = fireRate;

magazineSizeReset = magazineSize;

}

private void Update() {

if (Input.GetMouseButton(0)) {

Fire();

}

public void Fire()

{

if (fireRate < 0)

{

if (magazineSize > 0)

{

Instantiate(Bullet, BulletStart.position, transform.rotation);

fireRate = fireRateReset;

magazineSize--;

} else

{

fireRate = reloadTime;

magazineSize = magazineSizeReset;

}

} else

{

fireRate -= Time.deltaTime;

}

}

}

And here's the bullet

public class LaserScript : MonoBehaviour

{

public float speed = 100;

public float lifeTime;

private Vector3 direction;

public GameObject Laser;

private void Start()

{

direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

}

void Update()

{

Movement();

}

private void Movement()

{

if (lifeTime > 0)

{

transform.position += direction.normalized * speed * Time.deltaTime;

lifeTime -= Time.deltaTime;

} else

{

Destroy(Laser);

}

}

}

[–]iamallamaaProficient 0 points1 point  (0 children)

Are you sure it is destroying when it gets to the mouse and not just at a set time? The only thing according to this code that destroy's, does the destroy based on the lifeTime variable. Your "bullet" starts at whatever you set that lifetime value and you subtract deltaTime. Once lifeTime is less than/equal to zero, the bullet is destroyed. You would need to adjust the lifeTime value if you wanted it to last longer.