Question by
MrTinzie · Aug 30, 2015 at 12:05 AM ·
rotationprojectile
How can i get my projectile to face its direction of travel?
i know there are many questions similar to this, but none of the solutions have worked for me after 6 hours of searching. also im working in unity 5, which changes some things. basically i just want my projectile (which resembles a pill) to point in the direction its travelling. it moves to my mouse cursor when i click. this is my code so far:
note: the transform.rotation line comes up with a "an object referance is required for the non-static..." error.
private Rigidbody2D myRigidbody;
void Start()
{
camera = (Camera)GameObject.FindObjectOfType(typeof(Camera)); // get the camera
myRigidbody = GetComponent<Rigidbody2D>();
// get world location of click
clickPos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
Destroy(gameObject, 3); // give projectile 3 seconds lifetime, if they aren't already destroyed
transform.rotation = Quaternion.SetLookRotation(myRigidbody.velocity);
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, clickPos, speed * Time.deltaTime);
Debug.Log("cursor pos" + clickPos);
//Vector2 motionDirection = myRigidbody.velocity.normalized;
if(((Vector3)transform.position == clickPos))
{
Destroy(gameObject);
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612001949im_/https://answers.unity.com/themes/thub/images/avi.jpg)