- Home /
Mid air "sprint"
Hi all,
I've got a problem, as usual :\ .
I'd like to do that when I press the x key while going right or left (it's a 2D game) in mid air, I do a sort of sprint https://www.youtube.com/watch?v=Rw7pY03SixA like the one at 0:37.
if (Input.GetKey(KeyCode.RightArrow)) {
transform.Translate(Vector3(1, 0, 0) * Time.deltaTime * walkspeed);
if (Input.GetKeyDown("x") && sprintcount == 0) {
rigidbody.AddForce(10.0f * gameObject.transform.right, ForceMode.Impulse);
sprintcount +=1;
}
}
this is my code, but it doesn't work like I would like it to do. I'd like to make the character go straight like in the video.
You should consider using a character controler. It is the easiest way the have a character with properties such as velocity and acceleration, while still having some direct control over position. Look up character controler in the unity docs for an intro to using them.
Answer by DFledermaus · Oct 23, 2013 at 02:44 AM
You're combining Translate with AddForce, which causes unpredictable results.
You only use AddForce if you are going to use the physics engine with this game, if not then Translate is fine.
Answer by ProTheJoker · Oct 23, 2013 at 01:59 PM
You mean like this?
transform.Translate(Vector3(5,0,0), ForceMode.Impulse); because it doesn't work, it says No appropriate version of 'UnityEngine.Transform.Translate' for the argument list '(UnityEngine.Vector3, UnityEngine.ForceMode)' was found.
ok, I got it, you meant something like "transform.Translate( Vector3(5,0,0))" and it works
Your answer
Follow this Question
Related Questions
Double tap to run 1 Answer
What's wrong with my sprinting logic? 1 Answer
How do I script so my character Jumps farther when Running? 1 Answer
How do I add a sprint recharge 1 Answer