- Home /
Question by
Harardin · Nov 25, 2016 at 02:43 PM ·
c#scripting problemscriptingbasics
How to make an 2d object always look at the mouse position without using camera.
I have a triangle 2d sprite, I want to make it always face a mouse position with its higher edge.
Here is my code example: public float speed = 10.0f; private Vector3 pivot; // Use this for initialization void Start () {
}
// Update is called once per frame
void Update ()
{
// movement
pivot = new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0);
transform.Translate(pivot);
// rotating
transform.RotateAround(pivot, new Vector3(0, 0, 20 /*Just Input.mousePosition.z didn't help*/), speed * Time.deltaTime);
}
Comment
Forget to mention, it actualy rotating but not controlable.
Your answer
Follow this Question
Related Questions
Have some minor problems. Cant tell if its a script issue or a animator issue. 1 Answer
Spawning 3D Objects according to Data at JSON Matrix 0 Answers
Help with ontrigger enter and exit with UI Display appear and disappear 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers