Created
March 22, 2018 08:23
-
-
Save Nashet/c8a3c0eda4fb9b4781a4f2aa68880b97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Update () | |
{ | |
Vector2 mouseDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - (transform.position);//vector to mouse position | |
float curDir = SignCrossZ(curForward, mouseDir); //angle to mouse | |
float curDelta = curDir * speed * Time.deltaTime; //angle change speed | |
Vector2 newForward = Quaternion.Euler(0, 0, curDelta) * curForward; // new projection | |
if (SignCrossZ(curForward, newForward) != SignCrossZ(newForward, mouseDir)) { | |
// went wrong direction, change projection | |
curForward = Quaternion.Euler(0, 0, Vector2.SignedAngle(Vector2.up, mouseDir)) * Vector2.up; | |
} | |
else{//right direction | |
curForward = newForward; | |
} | |
//дебуг | |
//lrCurrent.SetPosition(0, transform.position); | |
//lrCurrent.SetPosition(1, (Vector2)transform.position+curForward * 4); | |
//lrTarget.SetPosition(0, transform.position + Vector3.forward); | |
//lrTarget.SetPosition(1, transform.position + Quaternion.Euler(0, 0, Vector2.SignedAngle(Vector2.up, mouseDir)) * Vector2.up * 5 + Vector3.forward); | |
// sets rotation | |
var angle = Mathf.Atan2(curForward.y, curForward.x) * Mathf.Rad2Deg - 90f; | |
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment