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

Character moving and rotation but not walking

Discussion in 'Scripting' started by Digypt-DigitalEgypt, Jun 5, 2021.

  1. Digypt-DigitalEgypt

    Digypt-DigitalEgypt

    Joined:
    Jun 1, 2021
    Posts:
    5
    Hi, my character is moving forward, back, etc. and rotating, but not moving its legs to walk.

    I have the following code. Can anybody tell me what I'm doing wrong? Thanks.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     // For Player Variables
    9.  
    10.     public float currentMoveSpeed;
    11.     public float moveSpeed;
    12.     public Vector3 velocity;
    13.     public float currentWalkAnimationSpeed;
    14.     public float walkAnimationSpeed;
    15.     public float rotationSpeed;
    16.  
    17.     // For Player Component
    18.  
    19.     private Rigidbody rb_player;
    20.     private Animator an_Player;
    21.  
    22.     // For Other GameObjects
    23.  
    24.     public GameObject cameraObj;
    25.     public GameObject playerStaticRotation;
    26.  
    27.     // Awake is called before the first frame update
    28.     void Awake()
    29.     {
    30.        
    31.         rb_player = GetComponent<Rigidbody>();
    32.         an_Player = GetComponent<Animator>();
    33.  
    34.     }
    35.  
    36.     // FixedUpdate is called once per frame
    37.     void FixedUpdate()
    38.     {
    39.  
    40.         // Check if top arrow and down arrow and left arrow and right arrow are pressed by keyboard .. if pressed move player
    41.  
    42.         if (Input.GetAxis("Vertical")  != 0.0f || Input.GetAxis("Horizontal") != 0.0f) {
    43.  
    44.             // Move Player
    45.  
    46.             MovePlayer(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"));
    47.  
    48.         }
    49.  
    50.     }
    51.  
    52.     // Move Function to Move Player
    53.  
    54.     public void MovePlayer(float forward, float right){
    55.  
    56.         // forward parameter to get virtical axis from keyboard , right parameter to get horizontal axis from keyboard
    57.         // get virtical axis and horizontal axis and multiple to camera vertical axis and horizontal axis
    58.  
    59.         Vector3 translation;
    60.         translation = forward * cameraObj.transform.forward;
    61.         translation += right * cameraObj.transform.right;
    62.         translation.y = 0;
    63.  
    64.         // Check if vertical and horizontal presssed
    65.  
    66.         if(translation.magnitude > 0.2f) {
    67.  
    68.             // set velocity equal to translation
    69.    
    70.             velocity = translation;
    71.  
    72.         } else {
    73.  
    74.             // set velocity to zero
    75.  
    76.             velocity = Vector3.zero;
    77.         }
    78.  
    79.         // Move Player by Rigidbody velocity
    80.  
    81.         rb_player.velocity = new Vector3(velocity.normalized.x * moveSpeed, rb_player.velocity.y, velocity.normalized.z * moveSpeed);
    82.  
    83.         // Rotate Player
    84.  
    85.         if(velocity.magnitude > 0.2f) {
    86.  
    87.             transform.rotation = Quaternion.Lerp(playerStaticRotation.transform.rotation, Quaternion.LookRotation(velocity), Time.deltaTime * rotationSpeed);
    88.  
    89.         }
    90.  
    91.  
    92.         // Move Animation
    93.  
    94.        an_Player.SetFloat("Velocity", velocity.magnitude * walkAnimationSpeed);
    95.  
    96.     }
    97.  
    98.  
    99. }
    100.  
    101.  
    102.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,816
    Sounds like animation is broken.

    Open the animator window while playing, start seeing what states it goes through.

    If it is changing properties but not states, go verify your transitions.

    If it fails to change properties, start sprinkling Debug.Log() statements throughout the above code to verify it is setting the properties correctly.
     
  3. Digypt-DigitalEgypt

    Digypt-DigitalEgypt

    Joined:
    Jun 1, 2021
    Posts:
    5
    Thanks for your reply, which led me to check all the settings.

    Turns out the Parameter setting was on "Blend." Changed it to "Velocity" and the character finally started moving his legs when walking.

    Now he takes two steps and hops back one step. :D
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,816
    That sounds like you're fighting with moving the object in script and at the same time still leaving root motion enabled on the animation. Usually you only want one of those going on.
     
  5. Digypt-DigitalEgypt

    Digypt-DigitalEgypt

    Joined:
    Jun 1, 2021
    Posts:
    5
  6. Digypt-DigitalEgypt

    Digypt-DigitalEgypt

    Joined:
    Jun 1, 2021
    Posts:
    5