- Home /
how do i stop the stuttering?
i'm trying to get an object to move a certain way. it's a spaceship i have it moving how i want it but it stutters as it moves. i want to have it point the direction that is pressed on the joystick and then the thrust button to move it forward. after the thrust is released i want it to float forward for a small distance in that direction but still be able to turn towards a different direction. any suggestions to get rid of the stuttering?
this is the code i have on my player object:
{
public float rotationSpeed = 450;
public float accelerationForce = 10f;
private Quaternion targetRotation;
void Start ()
{
}
void Update ()
{
float acceleration = Input.GetAxis ("Thrust");
rigidbody.AddForce (transform.forward * acceleration * accelerationForce);
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
if (input != Vector3.zero) {
targetRotation = Quaternion.LookRotation (input);
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed *Time.deltaTime);
}
}
}
sorry it took so long to respond. turns out it was being caused by the camera script. thanks for the help though!
Answer by tanoshimi · Nov 14, 2014 at 05:09 PM
Generally speaking, you should either choose to modify an object kinematically (i.e. you directly set transform.position
and transform.rotation
in the Update() loop), or you apply forces and let the physics engine resolve the corresponding position and rotation (i.e. use rigidbody.AddForce()
in FixedUpdate()).
You're doing a mixture of both, which can have interesting side effects. At the very least, you should move your AddForce() code into FixedUpdate() rather than Update(), and see if that makes a difference.
Well several things are might cause issues in your example code. Doing what is suggested in the answer would be an improvement. Perhaps add your improved version by editing the question or add a comment to the question. What kind of joystick are you using btw?
Also Input.GetAxisRaw does not take deadzone into consideration among other things
Answer by Addyarb · Nov 18, 2014 at 01:07 AM
If you press a key, transform.position = vector3.forward... if you let go of that key, let the velocity be a slerp between your current velocity and 0.
Let me know if you need this in code form!
Your answer
Follow this Question
Related Questions
move enemy to old position of player 2 Answers
The most effective way to search enemies. 1 Answer
Vector3.RotateTowards Problem 1 Answer