Player i spining when moving
I currently want to make a game for android, where you can move the player over an virtual joystick and if you press on the screen the player looks at the mouse. But i got the problem that if it looks at the mouse and then move it will just randomly spin. It's not caused of two touch inputs at the same time, because i had the same issue when i used my keyboard with W,A,S,D. For the Joystick i use the Standart Unity 2D Input. Thats the Script of the player:
// Update is called once per frame
void Update () {
//Movement
var mousepos_screen = Input.mousePosition; //Gets the Screen Cordinates of the mouse in pixels
var in_x = CrossPlatformInputManager.GetAxis("Horizontal"); //Returns 1;-1 if in this frame a key was pressed, thats in the "Horizontal" group
var in_y = CrossPlatformInputManager.GetAxis("Vertical"); //Returns 1;-1 if in this frame a key was pressed, thats in the "Vertical" group
myrigid.velocity = new Vector2(in_x, in_y) * speed * Time.deltaTime; //Sets the velocity of this Object to the Input; Gets multiplied by movementspeed and gamespeed
//Rotation
if (Input.GetMouseButton(0) && (mousepos_screen.x > 200 || mousepos_screen.y > 200))
{
//Debug.Log("MouseDown == true");
mousepos_screen.z = Camera.main.transform.position.z; //Sets the z component to the height of the player
var mousepos_world = Camera.main.ScreenToWorldPoint(mousepos_screen); //Translate the Screen Condinates to World Condinates
var dif = mousepos_world - transform.position; //Gets the Vector; (Spitze - Fuß)
var angle_rad = Mathf.Atan2(dif.y, dif.x); //Gets the Tangens in Radians
var angle_deg = Mathf.Rad2Deg * angle_rad; //Translate the Radians to Degrees
transform.rotation = Quaternion.Euler(0, 0, angle_deg - 269); //Rotate the Object
//Debug.Log("World: " + mousepos_world);
}
//Debug.Log("Screen: " + mousepos_screen);
}
I could also make a video of it and upload it to Youtube if you want. If anybody knows whats wrong with my script pls tell me! Thank you, Manuel Schmidbauer
Comment