Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Rotating transform to look at position in 2D
- //There is no need to license for 3 lines of code I hope
- //NOTE: This probably isn't the best solution performance-wise. It works pretty well tho.
- void Update(){
- float rotationSpeed = 50; //If you want it to be instant, delete rotationSpeed and Time.deltaTime. Not tested, but should work.
- Vector3 pointInSpaceAroundWhichIWantToRotate; //Possibly the position of the body of your character.
- Vector2 diff = MousePosition2D () - (Vector2)transform.position;
- float angle = Vector2.Angle (transform.up, diff);
- Vector3 vec = Vector3.Cross (transform.up, diff); //We need to determine the direction of diff. "transform.up" should be the way your character faces.
- if (vec.z > 0) {
- Char.head.transform.RotateAround (pointInSpaceAroundWhichIWantToRotate, Vector3.forward, angle * rotationSpeed * Time.deltaTime);
- } else {
- Char.head.transform.RotateAround (pointInSpaceAroundWhichIWantToRotate, Vector3.forward, -angle * rotationSpeed * Time.deltaTime);
- }
- }
- //NOTE: I use perspective camera in my 2D game, so I have to set the z position to 10. If you use ortographic, you don't have to do that.
- Vector2 MousePosition2D(){
- Vector3 mousePos = Input.mousePosition;
- mousePos.z = 10;
- return Camera.main.ScreenToWorldPoint (mousePos);
- }
Advertisement
Add Comment
Please, Sign In to add comment