- Home /
Velocity Behavior Question
Preface: Thank you to the people who took the time to answer my other post (redirects to here), however it seems to have fallen prey to an error and the comments/replies on it are not visible to me or a moderator.
Hello,
Please excuse me if this is not an appropriate question (i.e. specific enough to Unity) but I am asking in case it is due to something about the engine that I am not aware of. This is after looking up velocity in the broader sense, where speed is the absolute value of that vector and so on. With that definition in mind I am having trouble understanding the following behavior:
In writing my movement script I got to introducing the bool switch for "sprint". By increasing maxSpeedCurrent I was not getting to the sprinting cap, so I started playing with the value of force being applied on movement, from 15 to 1500. The cap works, however...
If I Debug.Log("Velocity: " + Mathf.Abs(rigBody.velocity.x)); I get up to 3, whether at 15 force or 1500 - the speed at which the character traverses space at that 3 value, however, is vastly different. How can it be that the same velocity represents different rates of movement? The above Debug snippet technically returns "speed" rather than velocity, but if I debug velocity I get a vector with one of its parameters behaving the same way: moving up to 3, no matter the force or speed.
Here is the code in case it helps to clarify (I read that modifying velocity per frame is not recommended and will look into that tomorrow - however it's going on 300am and I work tomorrow - I just mean that's not the focal issue here)
void FixedUpdate()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
sprint = Input.GetButton("Sprint");
if(horizontal == 0.0f && vertical == 0.0f) {inMovement = false;}
maxSpeedCur = maxSpeedBase;
if(sprint == true) maxSpeedCur += 1.5f;
if(horizontal != 0.0f)
{
if(horizontal > 0.0f) lastMoveInput = "right";
else if(horizontal < 0.0f) lastMoveInput = "left";
inMovement = true;
if(horizontal * rigBody.velocity.x < maxSpeedCur)
{
rigBody.AddForce(Vector2.right * horizontal * moveForce);
}
if(Mathf.Abs(rigBody.velocity.x) > maxSpeedCur)
{
rigBody.velocity = rigBody.velocity.normalized * maxSpeedCur;
}
}
Debug.Log ("Max Speed Current: " + maxSpeedCur);
Debug.Log ("Velocity Vector: " + rigBody.velocity);
Debug.Log ("Velocity X: " + Mathf.Abs(rigBody.velocity.x));
}
To give a concrete example of how I went about testing:
Setup: - Player Object (CircleCollider2D, Rigidbody2D, Linear Drag 5) - PlayerMovement script containing the above code, attached to Player Object - Cube andsphere background for reference
Test 1: - moveForce set to 15.0f; - Start game and move with D - Output is 3 velocity - Time taken to traverse cube platform is about 4 seconds.
Test 2: - moveForce set to 1500 - Start game and move with D - Output is still 3 velocity - Time taken to traverse cube platform is less than one second.
So, to reiterate why this is confusing: speed should be the magnitude of the velocity vector, but the same velocity value is giving different results.
Other Post for reference if admins need it to bug hunt (redircts to here): http://answers.unity3d.com/questions/1075183/understanding-velocity.html