Question by
Lookum · Jul 05, 2019 at 08:17 PM ·
rotationgameobjectdirection
Locking facing direction or changing it after following the mouse position
Hello, I have a GameObject that follow the mouse position when I click on the screen, everything works fine the Object rotate on Z axis, but in order to optimize the user experience I need to lock the direction of the object or after releasing my mouse button changing the direction to up
Here's a picture of what I want to do : https://ibb.co/HGLsWkm
Here's what I have, It's not what I want to do because it rotate the GameObject to the initial position after releasing the mouse but it can help
public Quaternion originalRotationValue;
if (Input.GetMouseButtonDown(0))
{
originalRotationValue = transform.rotation;
}
if (Input.GetMouseButton(0) && bounds.Contains(Input.mousePosition))
{
//transform.rotation = Quaternion.Euler(0, 0, gameObject.transform.rotation.z);
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
}
if (Input.GetMouseButtonUp(0))
{
transform.rotation = Quaternion.Slerp(transform.rotation, originalRotationValue, speed * Time.deltaTime);
}
Thanks !
Comment