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

Question Velocity-Based Movement?

Discussion in 'Scripting' started by Azmores, Jun 14, 2023.

  1. Azmores

    Azmores

    Joined:
    Apr 1, 2018
    Posts:
    4
    Hello and I hope everyone is doing well <3

    I've been trying to work on a movement system for a while now. I'm aiming for something that would feel like TLOZ Link Between Worlds as a base and while the Character Controller works really well for that, I'm unsure about using it due to other aspects of the game down the line. (One major mechanic I'm going to work towards is a Pounce either to an enemy or a position, but others might include using a hook as a grapple point, or even just basic wind physics needed).

    With all that in mind I've been doing some research for the past couple of days and the general direction I've moved towards is just to use the rigidbody velocity directly (AddForce added too much over time and I wanted more instant acceleration, and I'm avoiding the MoveDirection right now as I've heard it has a tendency to cause collision issues or override physics).

    I've actually gotten this to work in a rather hacky way (if velocity != 0 && no input then increase drag to a huge number for one physics update) but I'm hoping to find a more reliable and flexible way of making this work if possible. Code is below.

    I really appreciate all pieces of input possible. This is my first time really working on trying to make a movement system that is my own rather than one that is taken. I wouldn't be surprised if I have some manner of misconception of the systems I've mentioned as well and might need to return to a new style altogether.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine
    4.  
    5. public class PlayerController : MonoBehavior
    6. {
    7.     public float speed = 10f;
    8.  
    9.     private float vAxis = 0f;
    10.     private float hAxis = 0f;
    11.     private Rigidbody rb;
    12.  
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         vAxis = Input.GetAxisRaw("Vertical");
    21.         hAxis = Input.GetAxisRaw("Horizontal");
    22.     }
    23.  
    24.     private void FixedUpdate()
    25.     {
    26.         Vector3 movement = new Vector3(hAxis, 0, vAxis).normalized * speed * Time.fixedDeltaTime;
    27.         rb.velocity += movement;
    28.     }
    29.  
    30. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    There's a LOT of ways to go about this.

    To see what I mean about ways of doing things, here's a great primer:



    Go nuts, tell us what you find!
     
  3. Azmores

    Azmores

    Joined:
    Apr 1, 2018
    Posts:
    4
    I'll give this a study! I've sorta been throwing my mind directionlessly towards whatever I can find for the past 2 days so having some manner of "Check this out and see what you can learn from it" is better than what I had prior.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Nothing wrong with that... free form learning!!

    I have been throwing myself at games for 40+ years and I'm still finding new ways of combining stuff that is new and different!! Go nuts!

    Game dev is all about going "Hm, what if I did xxxx with yyyy... I wonder what that would do?" and then learning from it and having a great time.

    I like this guy's secret: just ask yourself again and again, "Can I... ?"

    Imphenzia: How Did I Learn To Make Games:

     
  5. Azmores

    Azmores

    Joined:
    Apr 1, 2018
    Posts:
    4
    Alright I've done some study... I've watched the video... I've watched a few more...

    My learning!

    Capsules are floating!

    Math is difficult!

    This is going to be so much more complicated than I thought!

    I love the movement they have and it looks very adjustable to any type of game so... I want to learn how to emulate that. I did find a unity asset that people were referencing in the video comments saying they learned what this video taught them in there. The bits of math they show is nice too but it's a bit difficult to comprehend without context to the rest of the space. I feel like first I might just work on making a capsule float and then move on from there... If all goes well then hopefully I'll be able to handle the movement next rather simply.
     
    Sluggy likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    THAT IS AWESOME!! So glad to hear it. I did his car traction one, not the character one fully yet. I gotta go back and just restart it afresh.

    This attitude will maximize your fun and ALSO maximize your learning. Be patient, have a really really fun time and you won't even recognize it as actual work. Gamedev is amazing.
     
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,913
    You can also set velocity directly. Instead of "increase drag to a huge number" you can use
    rb.velocity=Vector3.zero;
    .

    This also works for non-zero -- you could compute the velocity you want (maybe purely based on control position, or using that plus a fraction of old velocity to get a ramp-up effect) and then
    rb.velocity=v;
    . Basically, even though
    rb.velocity
    is a special physics value it can be used the same as any other Vector3. I've seen advice to not do this, but I've done it in dozens of different ways and never had a problem. The only odd thing is that velocity= and += changes it right away (obviously), whereas AddForce seems to save it up to add during the physics step.
     
  8. Azmores

    Azmores

    Joined:
    Apr 1, 2018
    Posts:
    4
    I've gotten advice around this too and the only reason I'm avoiding it is in case I want to, down the line, add something into the game (like heavy wind or whatnot) that would require an external force be applied to the player. Directly setting Velocity instead of adding to it would mean that this would be more cumbersome down the line. I may end up going that route anyways but I am taking stock of all options prior.