I’m trying to set up my scene so that when the user clicks and drags the mouse, it will place the player at the mouse down location and rotate the player to face the mouse up location. I have it worked out except for the rotation part. I have never really been clear on rotations anyway.
It seems like there a number of ways to use the rotation. I can set the rotation of the player when placing it to zero or looking right or whatever and then change it to the rotation created by the two input.mouselocations. But ow do I do that? Here is my code, thank you…
private Vector3 mousePosition1;
private Vector3 screenPosition1;
private Vector3 mousePosition2;
private Vector3 screenPosition2;
private float playerRotation;
private float playerY;
private float a = 1;
[HideInInspector]
public bool isActive;
public Transform player;
void Awake()
{
isActive = false;
}
void Start ()
{
playerY = player.transform.position.y;
}
void Update ()
{
if (isActive)
{
if (Input.GetMouseButtonDown (0))
{
mousePosition1 = Input.mousePosition;
screenPosition1 = Camera.main.ScreenToWorldPoint (mousePosition1);
screenPosition1.y = playerY;
player.transform.position = screenPosition1;
}
if (Input.GetMouseButtonUp (0))
{
mousePosition2 = Input.mousePosition;
screenPosition2 = Camera.main.ScreenToWorldPoint (mousePosition2);
screenPosition2.y = playerY;
float angle = Mathf.Atan2 (screenPosition1.y, screenPosition2.y);
player.transform.Rotate (Vector3.up * angle * a);
}
}
}