- Home /
2D 360 Shooting Is not working (Top Down, Shooting in all directions)
I am trying to make a top down in 2d and I have tried an unbelievable amount of scripts. Where I am now is a dot that is attached to the barrel but really far away. That dot is where I want the bullet to go and stop at that point. Everything works except for the actual moving of the bullet. Heres my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BulletsFiring : MonoBehaviour {
public Rigidbody2D projectile;
public Transform projectileSpawnPoint;
public float projectileVelocity;
public float timeBetweenShots;
private float timeBetweenShotsCounter;
private bool canShoot;
public GameObject Target;
// Use this for initialization
void Start () {
canShoot = false;
timeBetweenShotsCounter = timeBetweenShots;
}
// Update is called once per frame
void Update (){
if (Input.GetMouseButtonDown(0) && canShoot)
{
Rigidbody2D bulletInstance = Instantiate(projectile, projectileSpawnPoint.position, Quaternion.Euler(new Vector3(0, 0, transform.localEulerAngles.z))) as Rigidbody2D;
projectile.transform.position = Vector3.MoveTowards (transform.position, Target.transform.position, projectileVelocity);
canShoot = false;
}
if (!canShoot)
{
timeBetweenShotsCounter -= Time.deltaTime;
if(timeBetweenShotsCounter <= 0)
{
canShoot = true;
timeBetweenShotsCounter = timeBetweenShots;
}
}
}
}
Answer by TanselAltinel · May 08, 2018 at 08:47 PM
Hi
In order for the projectile to move, it should be called each update. However, in your script, you are calling MoveTowards
only once.
Look here: https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
Also, I'd suggest to move projectile with its own script instead of moving it from a manager.
Another thing I'd suggest is that, if your projectile is using RigidBody2D, then you should use RigidBody2D functions to move it.
This might work for you: https://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html