Search Unity

Rotating an object according to the way it's moving

Discussion in '2D' started by Jhirim, May 11, 2015.

  1. Jhirim

    Jhirim

    Joined:
    Feb 2, 2015
    Posts:
    6
    I am trying to rotate an object so the pointy end will be what hits the enemies. I instantiate it with AddForce and this is the code of the Shooting Script. I've tried with Quaternion and transform but I've maybe missed something important
    if (Input.GetKey(fireKey) && tm.canShoot)
    {
    //Makes the tank shoot
    GameObject missile = Instantiate(missilePrefab, transform.position + transform.up * 2f, transform.rotation) as GameObject;
    missile.GetComponent<Rigidbody2D>().AddForce(transform.up * missileForce);
    missile.GetComponent<MissileScript>().owner = gameObject;

    tm.canShoot = false;
    Invoke("Reload", 3f);
    }
    }
    public void Reload()
    {
    tm.canShoot = true;
    }
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    First things first: Using code tags properly

    Second, can you please show a screenshot of your current object (I assume it's the tank's cannon), and the missile, both with their default (Quaternion.identity) rotation? If both are set up correctly, using "transform.rotation" should give your missile the same rotation as the cannon, making it point the way it is shot.