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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rigidbody Rotating Towards Wall

Discussion in 'Physics' started by Wilelle, Mar 18, 2016.

  1. Wilelle

    Wilelle

    Joined:
    Jun 16, 2015
    Posts:
    93
    I got a weird issue where when my player walks into a wall and I try to turn him while holding down the walk key, he turns reluctantly a little bit and the moment I stop turning him he immediately returns to facing the wall. I want him to be able to walk along the wall and stuff since this will easily become an incredibly annoying issue.

    Here's my script for walking and rotating:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var moveSpeed = 0.0;
    4. var maxSpeed = 0.5;
    5. var accelerationSpeed = 1;
    6. var rotationSpeedLeft = 0.0;
    7. var rotationSpeedRight = 0.0;
    8. var accelerationSpeedRotation = 100.0;
    9. var maxSpeedRotation = 50.0;
    10. var movingForward : boolean;
    11. var rotatingLeft : boolean;
    12. var rotatingRight : boolean;
    13.  
    14. function Start ()
    15. {
    16.  
    17. }
    18.  
    19. function Update ()
    20. {
    21.     if (Input.GetKey(KeyCode.W))
    22.     {
    23.         GetComponent.<Rigidbody>().velocity = transform.forward * moveSpeed;
    24.         if (moveSpeed < maxSpeed)
    25.         {
    26.             moveSpeed += accelerationSpeed * Time.deltaTime;
    27.         }
    28.         movingForward = true;
    29.     }
    30.     if (!Input.GetKey(KeyCode.W) && movingForward == true)
    31.     {
    32.         if (moveSpeed > 0)
    33.         {
    34.             GetComponent.<Rigidbody>().velocity = transform.forward * moveSpeed;
    35.             moveSpeed -= accelerationSpeed * Time.deltaTime;
    36.         }
    37.         else
    38.         {
    39.             movingForward = false;
    40.         }
    41.     }
    42.  
    43.     if (Input.GetKey(KeyCode.A))
    44.     {
    45.         transform.Rotate(0, -rotationSpeedLeft * Time.deltaTime, 0);
    46.         if (rotationSpeedLeft < maxSpeedRotation)
    47.         {
    48.             rotationSpeedLeft += accelerationSpeedRotation * Time.deltaTime;
    49.         }
    50.         rotatingLeft = true;
    51.     }
    52.     if (!Input.GetKey(KeyCode.A) && rotatingLeft == true)
    53.     {
    54.         if (rotationSpeedLeft > 0)
    55.         {
    56.             transform.Rotate(0, -rotationSpeedLeft * Time.deltaTime, 0);
    57.             rotationSpeedLeft -= accelerationSpeedRotation * Time.deltaTime;
    58.         }
    59.         else
    60.         {
    61.             rotatingLeft = false;
    62.         }
    63.     }
    64.  
    65.     if (Input.GetKey(KeyCode.D))
    66.     {
    67.         transform.Rotate(0, rotationSpeedRight * Time.deltaTime, 0);
    68.         if (rotationSpeedRight < maxSpeedRotation)
    69.         {
    70.             rotationSpeedRight += accelerationSpeedRotation * Time.deltaTime;
    71.         }
    72.         rotatingRight = true;
    73.     }
    74.     if (!Input.GetKey(KeyCode.D) && rotatingRight == true)
    75.     {
    76.         if (rotationSpeedRight > 0)
    77.         {
    78.             transform.Rotate(0, rotationSpeedRight * Time.deltaTime, 0);
    79.             rotationSpeedRight -= accelerationSpeedRotation * Time.deltaTime;
    80.         }
    81.         else
    82.         {
    83.             rotatingRight = false;
    84.         }
    85.     }
    86. }
    (As you can see I've added a damping effect to all movement. I don't know if that's part of the issue, but I'm mentioning it just in case all the extra code is confusing)
     
    Last edited: Mar 18, 2016
  2. Wilelle

    Wilelle

    Joined:
    Jun 16, 2015
    Posts:
    93
    Nevermind, I found a solution.