How can I stop running in place when I hold shift?

I do a FirstPersonController and when I press shift and I dont pressed any other key. My character do the running animation. But I dont want it. I want do running animation when I move. Plz Help me :slight_smile:

I have a characterController attach to my Player GameObject and my FirstPersonControllerScript.

I use Unity 2019.2.4f

Hello

Then stop the Animation. It is impossible to help you cause we cant sSee anything.
Christoph w

1 Like

This is my code… I dont think my other code is needed because is just a MouseLook and escuse me for to much line. I didn’t not erease them all… is in a practice and thank you an advance. Escuse me for my english to :slight_smile:

5949638–637334–FirstPersonController.cs (6.07 KB)

I try a solution in this code with search the lastposition and current position but I dont know how to say to Unity in the if(currentPosition == lastPosition after certain of time)
Do…Nothing or something like that.

I get a red Ligne each time I try to get two thing and say is Équal to himself

Read this:

Then copy and paste your code up, people will help then - I looked and you don’t have anything that sets isRunning to false in your code, only

playerHeadAnimator.SetBool("isRunning", true);

I dont have isRunning false because I dont know how to check if the player is in place and I see a lot of post with the codeFile download but its ok…

using UnityEngine;

public class FirstPersonController : MonoBehaviour
{
    [Header("Check if the player move")]
    public Vector3 lastPos;
    public Transform player;
    public float threshold = 0.0f;  //minimum displacement to recognize.

    [SerializeField] private CharacterController controller;

    [Header("Interaction")]
    [SerializeField] private GameObject gameCamera;
    [SerializeField] private KeyCode interactionKey;
    [SerializeField] private float interactionDistance;

    [Header("Movement control")]
    [SerializeField] private KeyCode crouchKey = KeyCode.LeftControl;
    [SerializeField] private KeyCode runKey = KeyCode.LeftShift;
    [SerializeField] private Animator playerHeadAnimator;

    [Header("Height Check")]
    [SerializeField] private float originalHeight;
    [SerializeField] private float crouchHeight;

    [Header("Physics")]
    [SerializeField] private float speed = 5f;
    [SerializeField] private float gravity = -9.81f;
    [SerializeField] private float jumpHeight = 3f;

    [Header("Ground Check")]
    public Transform groundCheck;
    [SerializeField] private float groundDistance = 0.4f;
    [SerializeField] private LayerMask groundMask;

    Vector3 velocity;

    public bool isRun;
    public bool isJump = false;
    public bool isCrouch;
    public bool isGrounded;

    public bool canWalk;
    public bool canRun;
    public bool canJump;
    public bool canMove;

    public bool positionIsChange;
    public bool moveButtonIsPressed;

    private void Start()
    {
        lastPos = player.position;

        canWalk = true;
        canRun = true;
        canJump = true;
        canMove = true;
        originalHeight = controller.height;
    }

    private void Update()
    {
        Vector3 offset = player.position - lastPos;

        if (offset.x > threshold)
        {
            // update lastPos
            lastPos = player.position;

            //code to execute when X is getting bigger
            moveButtonIsPressed = true;
            positionIsChange = true;
        }
        else if (offset.x < -threshold)
        {
            // update lastPos
            lastPos = player.position;

            // code to execute when X is getting smaller
            moveButtonIsPressed = true;
            positionIsChange = true;
        }

        if (moveButtonIsPressed == false && positionIsChange == false)
        {
            isRun = false;
        }

        if (Input.anyKey == false)
        {
            playerHeadAnimator.SetBool("isRunning", false);
        }

        if (canJump == true  && canWalk == true || canMove == true && canWalk == true)
        {
            Move();
        }

        if (Input.GetKeyDown(crouchKey) && isGrounded == true)
        {
            isCrouch = !isCrouch;
            CheckCrouch();
        }

        if (Input.GetKeyDown(runKey) && isGrounded == true && canRun == true)
        {
            isCrouch = false;
            controller.height = originalHeight;
            Run();
        }

        if (Input.GetKeyUp(runKey) && isGrounded == true)
        {
            isRun = false;
            canRun = true;
            playerHeadAnimator.SetBool("isRunning", false);
            speed = 5f;
        }

       

        // Interaction logic.
#if UNITY_EDITOR
        // Draw interaction line.
        Debug.DrawLine(gameCamera.transform.position, gameCamera.transform.position + gameCamera.transform.forward * interactionDistance, Color.green);
#endif
        if (Input.GetKeyDown(interactionKey))
        {
            RaycastHit hit;
            if (Physics.Raycast(gameCamera.transform.position, gameCamera.transform.forward, out hit, interactionDistance))
            {             
                if (hit.collider.tag == "BigCrossDoor")
                {
                    Animator anim = hit.collider.gameObject.GetComponent<Animator>();
                    if (anim != null)
                    {
                        anim.SetBool("openBigCrossDoor", true);
                    }
                }
               
                if (hit.collider.tag == "WoodChest")
                {
                    Animator anim = hit.collider.gameObject.GetComponent<Animator>();
                    if (anim != null)
                    {
                        anim.SetBool("openWoodChest", true);
                    }
                }
            }
        }
    }

    public void Move()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0)
        {
            isJump = false;
            canJump = true;
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * y;

        controller.Move(move * speed * Time.deltaTime);

        Jump();
    }

    void Jump()
    {
        if (Input.GetKeyDown("space") && isGrounded && canJump)
        {
            isJump = true;
            isRun = false;
            canRun = false;
            canJump = false;
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }

    void CheckCrouch()
    {
        if(isCrouch == true)
        {
            speed = 3f;
            canRun = false;
            canJump = false;
            controller.height = crouchHeight;
           
        }
        else if (isCrouch == false)
        {
            speed = 5f;
            canRun = true;
            canJump = true;
            controller.height = originalHeight;
        }
    }

    void Run()
    {
        isRun = true;

        if (isRun == true && isGrounded == true && isJump == false && isCrouch == false)
        {
            playerHeadAnimator.SetBool("isRunning", true);
            speed = 8f;
        }
    }
}

Just learn to read my question… its not difficult. You cant respond me because you dont see my code. But its bizzare when the moment I show my code. Dont have an anwser. I post my code because some guy’s is like to be a princess and I dont have an answer. All programmer is a joke into Unity? I explain the essencial. just explain your logic to my problem. You dont need my code for this.

Have a look at animation states and animation transitions. Specifically, transition properties.
A simple & common way to do this is by having your character blend between an idle and moving animation depending on their velocity.

I.E:
You could create a “movementSpeed” float property for your animator, and update it using the magnitude of the “offset” Vector3 you’re creating here:

Vector3 offset = player.position - lastPos;

Like this:

Vector3 offset = player.position - lastPos;
playerHeadAnimator.SetFloat("movementSpeed", offset.magnitude);

Then add a transition between your idle animation and running animation using the “movementSpeed” property as the condition.

  • If “movementSpeed” < x, transition to the idle animation.
  • If “movementSpeed” > x, transition to the running animation.

You could additionally multiply the running animation’s playback speed with the “movementSpeed” property to play the animation faster/slower depending on how fast the character is moving in the scene.

Thank you Vryken, I will try this after school :slight_smile: Good day to you

I put this in my Update and I comment where I get an error:

Vector3 offset = player.position - lastPos;
playerAnimator.SetFloat(“movementSpeed”, offset.magnitude);

if (offset.magnitude < x) //A got an error for the x value. The error message is : The name x does not exist in the context.
{
playerAnimator.SetBool(“isRunning”, false);
}

oh ok, my value x is in fonction Move()

float x = Input.GetAxis(“Horizontal”);
float y = Input.GetAxis(“Vertical”);

I put this after and I got my x value. But after that is not the same value of my parameter in the animator float “movementSpeed”. How I can get the x value.

I can try to put the float x in my parameter of the animator?

“x” was just a random placeholder value you could specify in your animator conditions.
This tutorial here shows what I’m talking about:

my value wont change :frowning:

I find thank you :slight_smile: the solution is implement this I think:

playerAnimator.SetFloat(“movementSpeed”, Input.GetAxis(“Horizontal”));
playerAnimator.SetFloat(“movementSpeed”, Input.GetAxis(“Vertical”));

my value change because I put lines.

how to put a condition for this (I change the parameter name) :

playerAnimator.SetFloat(“xHoriz”, Input.GetAxis(“Horizontal”));
playerAnimator.SetFloat(“yVert”, Input.GetAxis(“Vertical”));

because if statement dont accept this ligne of code:

if (“xHoriz” && “yVert” > 0)
…

I need else if condition for my game but I find a solution :smile:

f (xHoriz == 0)
{
isRun = false;
canRun = false;
playerAnimator.SetBool(“isRunning”, false);
}
if (yVert == 0)
{
isRun = false;
canRun = false;
playerAnimator.SetBool(“isRunning”, false);
}