- Home /
Top down shooter, bullet movement issues
this is the code ran at instantation of the object.
public class Shoot : MonoBehaviour {
public float speed;
Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = gameObject.GetComponent<Rigidbody2D>();
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Get mouse position
Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward); // set rotation to mouse position
transform.rotation = rot; //rotates game object to correct position
transform.eulerAngles = new Vector3(0,0,transform.eulerAngles.z); //remove x and y rotation along the x and y axis
Debug.Log(Input.mousePosition.normalized);
rb.AddForce(mousePosition.normalized*speed);
}
}
when the object is shot, sometimes the shot goes wide of the mouse cursor, as though some outside force is affecting it
if(Input.GetKeyDown(KeyCode.Space))
{
Instantiate(toFire,shotSpawn.position,shotSpawn.rotation );
}
This is the instantation code in my player
and I cant figure it out. If anyone has any top down 2d shooter tutorials that explain a better way to shoot let me know. Thanks, Sammy
Take a look at this question. It explains how to rotate 2D objects properly. As for your ai$$anonymous$$g, raw mouse position will not suffice. You need to perform raycasting from the camera, based on mouse position.
Answer by saravanan-P · Jun 11, 2015 at 05:42 AM
use this code for rotating the gun at the direction of the mouse pointer,
public int rotation_on_set = 0;
// Update is called once per frame
void Update ()
{
// subtracting the position of the player from the mouse position
Vector3 diff = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
diff.Normalize (); // normalizing the vector. Meaning that all the sum of the vector will be equal to 1
float rotZ = Mathf.Atan2 (diff.y, diff.x) * Mathf.Rad2Deg; // find the angle in degrees
transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotation_on_set);
}
for the bullet, create it as a prefab and give this script with a collider 2D,
public float bul_speed;
void Update ()
{
transform.Translate(Vector3.right * bul_speed);
Destroy(this.gameObject, 3.0f);
}
And for shooting the bullet when mouse button is pressed, Create a gameobject with the name "Shooting" to eject the bullet from that place, keep this "Shooting" gameobject as a child gameobject of the gun, so that bullet can be seen as it is coming from the gun..
public float fireRate = 0;
float timeToFire = 0;
Transform Shooting;
void Awake ()
{
Shooting = transform.FindChild ("Shooting");
}
void Update ()
{
if (fireRate == 0) // Fire rate is single burst
{
if (Input.GetButtonDown ("Fire1"))
{
Shoot();
}
}
else
{
if (Input.GetButton ("Fire1") && Time.time > timeToFire)
{
timeToFire = Time.time + 1/fireRate;
Shoot();
}
}
}
public GameObject Bulletq;
void Shoot ()
{
Vector2 ShootingPosition = new Vector2 (Shooting.position.x, Shooting.position.y);
Instantiate(Bulletq, new Vector3(ShootingPosition.x, ShootingPosition.y, 0), transform.rotation);
}
I'll try your code to see of it works better than my fix, thanks!
Answer by Yuanfeng · Jun 11, 2015 at 08:53 AM
screentoworldpoint function require the camera type set to orthogonal , if not it may be return the vector3.zero. Check If you had set the right type of camera. Secondly. Adforce need call each per frame. it isn't work with simple one call. Maybe you can had a try by change the velocity directly.
Answer by kekeoki · Jun 11, 2015 at 09:05 AM
My fix void Start () {
rb = gameObject.GetComponent<Rigidbody2D>();
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Get mouse position
Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward); // set rotation to mouse position
transform.rotation = rot; //rotates game object to correct position
transform.eulerAngles = new Vector3(0,0,transform.eulerAngles.z); //remove x and y rotation along the x and y axis
Debug.Log(Input.mousePosition.normalized);
rb.velocity =transform.up*speed;
}