- Home /
Sprite rotation clockwise or counter clockwise according to direction towards mouse
I made a small code that would rotate a sprite around an object according to the mouse position though the sprite would only move one way correctly and not the other way (counter clockwise it works fine but not clockwise) and I know it is a problem with the axis in the last line of code though I can't figure it out. Any help is accepted.PS:rb is the object to rotate around and rr is the object rotating
using System.Collections; using System.Collections.Generic; using UnityEngine; public class sprite : MonoBehaviour { public GameObject rb; private Vector2 mpo; public GameObject rr; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { mpo = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 mousep = new Vector2(mpo.x, mpo.y); Vector2 pp= new Vector2(rb.transform.position.x, rb.transform.position.y); Vector2 v1 = pp - mousep; Vector2 v2 = pp - new Vector2(rr.transform.position.x, rr.transform.position.y); float angle = Vector2.Angle(v1.normalized, v2.normalized); transform.RotateAround(rb.transform.position,transform.forward, angle); } }
Your answer
Follow this Question
Related Questions
rotating objects around collider axis 1 Answer
Rotation that keeps upright 1 Answer
Rotate objects face to camera 1 Answer
Locking rotation makes object spin 0 Answers
How to rotate an object according to the camera's view? 1 Answer