- Home /
Problem is not reproducible or outdated
Add force lasting 1 frame (simple script)
I figured out the problem. If anyone else has this problem here is how to fix it. Make sure Apply root motion is turned off in your animator.
(Sorry, i wasn't clear with this post so ill try to be a little clearer. The velocity on the x axis is resting itself to 0 a single frame after I add the force. Even if I set in to Input.GetKey() the second i release the key all momentum on the x-axis stops.
Basically, the problem is that my velocity resets itself every frame on the X-axis.)
Hello again,
I recently asked a question very similar to this but I posted my full script. I decided to make a demo to see if the problem of my velocity only lasting one frame on the y axis would persist, and it did. here is the simple script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleTestMovement : MonoBehaviour {
public Rigidbody mainrb;
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
print(mainrb.velocity);
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D))
{
speed = 20 * (Input.GetKey(KeyCode.A) || Input.GetKeyDown(KeyCode.A) ? -1 : 1);
mainrb.AddForce(new Vector3(speed, speed, 0), ForceMode.Impulse);
}
}
}
This is the entire script which means that there must be something wrong in the editor. My player had a capsule collider and a rigidbody attached, the rigidbody has no freezes on position and the capsule collider does not have a physics material that could be messing something up. This is only happening in the x axis, the y axis works as it should. If anyone knows anything I could try I would appreciate a point in the right direction. Thanks!
It should not completely slow down on the next frame, but that depends on your settings. What is the mass of the rigidbody? what is the speed variable set too? What is drag set to?? All this will have to be taken into account. Try applying more force and changing the force mode for example to see where the difference lies if any.
Answer by NoCandyIncluded · Jun 01, 2019 at 03:03 PM
On line 16, Input.GetKeyDown() gets when the key is pressed, so everything in that code will only execute during the frame that you pressed the button. If you want the code to be executed during the whole time your pressing your key down, try, Input.GetKey()
Sorry, i wasn't clear with this post so ill try to be a little clearer. The velocity on the x axis is resting itself to 0 a single frame after I add the force. Even if I set in to Input.Get$$anonymous$$ey() the second i release the key all momentum on the x-axis stops.
Basically, the problem is that my velocity resets itself every frame on the X-axis.