Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Smoothly rotate player

Discussion in 'Editor & General Support' started by Josh-Allen, Nov 7, 2019.

  1. Josh-Allen

    Josh-Allen

    Joined:
    Apr 27, 2013
    Posts:
    5
    I have player Y rotation on joystick input working, but I am trying to figure out how to make it happen smoothly- right now the player snaps to the rotation angle I want. if anyone could help me, I would appreciate it.

    Code (CSharp):
    1. // Move/rotate Player
    2.         if (count == 0) {     //we are behind the player
    3.             float rotation = 0.0f;
    4.             float pyTarget = Input.GetAxis (Stick + " Stick Horizontal") * targetAngle;
    5.             float pxTarget = Input.GetAxis (Stick + " Stick Horizontal") * targetAngle;
    6.  
    7.             rotation += pyTarget;    //add joystick movement to player rotation
    8.             var playerMove = new Vector3 (pxTarget, 0, 0); //set X movement to stick input
    9.             Player.transform.eulerAngles = new Vector3 (0, rotation, 0); //rotate the player in direction of stick
    10.             Player.transform.position += (playerMove * (Time.deltaTime/damp)); //move player in direction of stick
    11.             Vector3 PlayerPos = Player.transform.position; //store player position
    12.  
    13.             // bind player to center area of screen
    14.             if (Player.transform.position.x <= -6.4f) {
    15.                 PlayerPos.x = -6.4f;
    16.             }
    17.             if (Player.transform.position.x >= -3.4) {
    18.                 PlayerPos.x = -3.4f;
    19.             }
    20.             PlayerPos.z = -5.5f;// lock Z axis to prevent drift
    21.             Player.transform.position = PlayerPos;
    22.         }
     
  2. Josh-Allen

    Josh-Allen

    Joined:
    Apr 27, 2013
    Posts:
    5
    NVM, this is actually working fine, I just need to add a damper to the speed of the rotation!