- Home /
Strafe movement speed
My vertical and strafe axis are setup w/ the same paremters, except for the keys, in Input manager. This code moves me as expected on Vertical, but it goes extremely fast on Strafe, movespeed is the same. Why the turbo boost on sidemove?
// movement code - turn left/right with Horizontal axis:
myTransform.Rotate (0, Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime, 0);
// update surface normal and isGrounded:
ray = new Ray (myTransform.position, -myNormal); // cast ray downwards
if (Physics.Raycast (ray, out hit)) { // use it to update myNormal and isGrounded
isGrounded = hit.distance <= distGround + deltaGround;
surfaceNormal = hit.normal;
} else {
isGrounded = false;
// assume usual ground normal to avoid "falling forever"
surfaceNormal = Vector3.up;
}
myNormal = Vector3.Lerp (myNormal, surfaceNormal, lerpSpeed * Time.deltaTime);
// find forward direction with new myNormal:
Vector3 myForward = Vector3.Cross (myTransform.right, myNormal);
// align character to the new myNormal while keeping the forward direction:
Quaternion targetRot = Quaternion.LookRotation (myForward, myNormal);
myTransform.rotation = Quaternion.Lerp (myTransform.rotation, targetRot, lerpSpeed * Time.deltaTime);
// move the character forth/back with Vertical axis:
if(Input.GetAxis("Vertical") != 0)
myTransform.Translate (0, 0, Input.GetAxis ("Vertical") * moveSpeed * Time.deltaTime);
if(Input.GetAxis("Strafing") != 0) {
myTransform.Translate (-(Input.GetAxis("Strafing")),0,0 * moveSpeed * Time.deltaTime);
}
Character has Rigidbody, useGravity false instead this is applied:
rigidbody.AddForce(-gravity*rigidbody.mass*myNormal);
Are you sure it's actually faster and doesn't just appear faster because of the way your camera is positioned? Try measuring in units how much it actually moves over a given timespan (say one second) in vertical/strafing.
If this code is for your player controls you better should use CharacterController.$$anonymous$$ove, or rigidbody.velocity, otherwise your player will not react any colliders while moving.
@tbkn it's not just appearance, the x and z values are very different
@nbg_yalta definitley not CC, but maybe rigidbody; would like to understand before I refactor this bit of movement. Also, as I said originally I have a rigidboy so transform.Translate works with colliders.
Answer by getyour411 · Jan 15, 2014 at 06:10 AM
I merged/changed it to this
myTransform.Translate (-Input.GetAxis("Strafing") * .1f,0,Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime);
I don't understand why I had to take the Strafe Axis and reduce it sharply via that *.01; I turned on Debug.Log to see the GetAxis values and it was 1 or -1 just like Vertical, but anyway I'm able to move on.
I don't understand why it moves different values in strafe than forward, but it must be something specific to your game because I've used it hundreds of times and it worked fine. You'd probably want to find the root cause because it could cause other problems you're not even aware of (especially if this is planned to be a commercial game).
Anyway, even going with your solution of giving different speed values, I'd recommend to at least multiply by Time.deltaTime, otherwise you will have different speeds in different framerates, ie machine and load depenedent speeds.
Agreed, that's actually part of this line you just have to scroll out to the right to see it
Only the Z is multiplied by the frame time, you should multiply the X as well...
Answer by RevengeOfTheFrog · May 03, 2014 at 12:57 AM
It's a parenthesis problem (It's easier to see if you format it like this): The reason why .1 probably looks like works is because it's a close enough value to moveSpeed * Time.deltaTime
What you have:
if(Input.GetAxis("Vertical") != 0)
myTransform.Translate (
0,
0,
Input.GetAxis ("Vertical") * moveSpeed * Time.deltaTime
);
if(Input.GetAxis("Strafing") != 0) {
myTransform.Translate (
-(Input.GetAxis("Strafing")),
0,
0 * moveSpeed * Time.deltaTime
);
That second part should be:
if(Input.GetAxis("Strafing") != 0) {
myTransform.Translate (
-(Input.GetAxis("Strafing")) * moveSpeed * Time.deltaTime,
0,
0
);
Your answer
Follow this Question
Related Questions
instantiate,destroy,gain speed in time 2 Answers
Moving multiple transforms from an array in a single script 0 Answers
Tranform.translate does not have constant speed 1 Answer
Match position of two object without making one a child of the other 4 Answers
Wait a fraction of a second within a Function Update() 1 Answer