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

Player Running in place

Discussion in 'Editor & General Support' started by hesanda80, Nov 14, 2019.

  1. hesanda80

    hesanda80

    Joined:
    Jul 12, 2012
    Posts:
    221
    When I hit w key in game, the player runs but only in place. Character.png
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     Rigidbody rb;
    8.     float speed = 50.0F;
    9.     float rotationSpeed = 50.0F;
    10.     Animator animator;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         rb = this.GetComponent<Rigidbody>();
    16.         animator = GetComponentInChildren<Animator>();
    17.  
    18.        
    19.     }
    20.  
    21.  
    22.     // Update is called once per frame
    23.     void LateUpdate()
    24.  
    25.  
    26.     {
    27.         float translation = Input.GetAxis("Vertical") * speed;
    28.         float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
    29.         translation *= Time.deltaTime;
    30.         rotation *= Time.deltaTime;
    31.         Quaternion turn = Quaternion.Euler(0f, rotation, 0f);
    32.         rb.MoveRotation(rb.rotation * turn);
    33.         animator.SetFloat("speedMuilt", translation);
    34.  
    35.         if (translation != 0)
    36.             animator.SetBool("Idling", false);
    37.         else
    38.             animator.SetBool("Idling", true);
    39.  
    40.         if (Input.GetKeyDown("space"))
    41.         {
    42.             animator.SetTrigger("Attacking");
    43.         }
    44.  
    45.         if (translation != 0)
    46.             animator.SetBool("Idling", false);
    47.         else
    48.             animator.SetBool("Idling", true);
    49.  
    50.         if (Input.GetKeyDown("w"))
    51.         {
    52.             animator.SetTrigger("Run");
    53.         }
    54.     }
    55. }
    56.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So where is the code you use to move the player forward? The code you've posted, the only thing you do when the "w" key is pressed is trigger an animation. So it isn't surprising an animation is all you're seeing.
     
  3. hesanda80

    hesanda80

    Joined:
    Jul 12, 2012
    Posts:
    221
    How do I get the player to move. I followed youtube video for this script.
     
  4. junctionboss

    junctionboss

    Joined:
    May 11, 2014
    Posts:
    227
    I'm having the same problem, but with Ethan non AI 3rd person character , any idea why ?
    Using 2019.2.17f1.
     
  5. ArtyUwU

    ArtyUwU

    Joined:
    Dec 27, 2018
    Posts:
    15
    If you are using root motion check if the root transform position XZ of the animation is not baked into position
    Otherwise if not root motion use
    rb.MovePosition(transform.position + (translation * transform.forward);
    after rb.MoveRotation