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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Character velocity based movement

Discussion in 'Scripting' started by pmatt1988, Jan 20, 2018.

  1. pmatt1988

    pmatt1988

    Joined:
    Jan 20, 2018
    Posts:
    4
    I am trying to create velocity based movement using a character controller and I am running into a problem.
    My Goal:
    I want to make sure that player input isn't increasing the velocity of the controller if the velocity is already greater than my characters max speed.

    My problem:
    With the below code, I have been able to meet my goal. Except, now an issue has arisen.
    if the characters velocity is greater than the max speed, then the player has no control over the character at all. I would like if the player moves in an apposing direction that it decreases the velocity of the character, even if the velocity of the character is over the max speed.

    I hope all of this has made sense....
    I have a feeling that I am greatly over complicating everything

    This is a mock up of what I have done so far and doesn't reflect all of my movement code.
    Code (CSharp):
    1. public void Update()
    2.     {
    3.         //I don't want to take the y of velocity into account when measuring the speed of the Character for the            //purposes of player input
    4.         Vector3 velocityXZ = new Vector3(velocity.x, 0, velocity.z);
    5.         if (velocityXZ.sqrMagnitude > speed * speed)
    6.         {
    7.             Vector3 dir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    8.             inputVelocity = dir * speed * Time.deltaTime;
    9.             Vector3 velocityChange = velocity + inputVelocity;
    10.             if (velocityChange.sqrMagnitude > speed * speed)
    11.                 velocity = inputVelocity - velocity;
    12.             else
    13.                 velocity += inputVelocity;
    14.          
    15.             controller.Move(velocity * Time.deltaTime);
    16.  
    17.         }
    18.     }
     
    Last edited: Jan 20, 2018
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    You are better off using a rigidbody instead of a character controller if you are going to involve physics
     
  3. pmatt1988

    pmatt1988

    Joined:
    Jan 20, 2018
    Posts:
    4
    Ok then how would you go about solving this problem for a controller using a rigidbody?

    My system is in place for a CharacterController and works as well as a Rigibody would for my application. I am at a crossroads where I would have the exact same problem with either.

    If I were using a rigidbody I would still want it to respect external forces and not have player control speed it up past the characters maximum movement speed.

    This has been cobbled together at work and I haven't been able to test it but it's the general gist....

    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         Vector3 velocity = rb.velocity
    4.         //I don't want to take the y of velocity into account when measuring the speed of the Character for the            //purposes of player input
    5.         Vector3 velocityXZ = new Vector3(velocity.x, 0, velocity.z);
    6.         if (velocityXZ.sqrMagnitude > speed * speed)
    7.         {
    8.             Vector3 dir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    9.             inputVelocity = dir * speed * Time.deltaTime;
    10.             Vector3 velocityChange = velocity + inputVelocity;
    11.             if (velocityChange.sqrMagnitude > speed * speed)
    12.                 rb.Addforce(inputVelocity - velocity, ForceMode.VelocityChange);
    13.             else
    14.                 rb.Addforce(inputVelocity, ForceMode.VelocityChange);
    15.  
    16.            
    17.  
    18.         }
    19.     }
     
    Last edited: Jan 20, 2018
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  5. pmatt1988

    pmatt1988

    Joined:
    Jan 20, 2018
    Posts:
    4
    I have tried using ClampMagnitude. It works well other than the fact that when I move my character while it's being affected by an external force ( an explosion for example) it is then snapped to the speed of the characters movement when the player attempts to move...
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Have a "local" velocity that is entirely input dependent and a "world" velocity that allows outside influences such as boost pads or moving platforms. Clamp the local velocity and combine it with the world velocity for the player's final velocity.
     
    zevonbiebelbrott and pmatt1988 like this.
  7. pmatt1988

    pmatt1988

    Joined:
    Jan 20, 2018
    Posts:
    4
    That does seem like the simplest solution. I'll give it a shot.
     
  8. zevonbiebelbrott

    zevonbiebelbrott

    Joined:
    Feb 14, 2021
    Posts:
    98
    Can you please post an example, im pulling out my hairs trying to get this to work
     
  9. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This thread is over two years old mate. Make a new one with your exact issue and the code you've tried.
     
  10. zevonbiebelbrott

    zevonbiebelbrott

    Joined:
    Feb 14, 2021
    Posts:
    98
    ok ill do that tomorrow, thanks for replying