- Home /
Player shaking when colliding with objects
Hi! I've recently encountered a problem with my player GameObject's script. As the question says, my object is shaking everytime it moves towards any object. Is there some way I can stop it from doing so? Here is my script:
var MoveSpeed : float = 2.5f;
var RunSpeed : float = 5f;
var Forward : float = 3.5f;
var JumpHeight : float = 10000f;
var Jumped = false;
function Start ()
{
rigidbody.freezeRotation = true;
}
function Update ()
{
if(Input.GetKey(KeyCode.Space) && !Jumped)
{
Jumped = true;
rigidbody.AddForce(Vector3.up * JumpHeight * Time.deltaTime);
}
//Player movement
if(Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * Forward * Time.deltaTime);
if(Input.GetKey(KeyCode.S))
transform.Translate(Vector3.back * MoveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.D))
transform.Translate(Vector3.right * MoveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.A))
transform.Translate(Vector3.left * MoveSpeed * Time.deltaTime);
//Player run movement
if(Input.GetKey(KeyCode.LeftShift))
{
if(Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * Forward * Time.deltaTime);
if(Input.GetKey(KeyCode.S))
transform.Translate(Vector3.back * MoveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.D))
transform.Translate(Vector3.right * MoveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.A))
transform.Translate(Vector3.left * MoveSpeed * Time.deltaTime);
}
}
function OnTriggerStay ()
{
Jumped = false;
}
That's all! I don't know if its because I'm using Translate. Sorry, I've only started Unity about a week ago.
Ahh! Thank you so much! It's all fixed now! (If only I could turn your comment into the answer!)
I've converted the comment into an answer and marked it as correct for you.
Answer by KingDolphin · Apr 05, 2014 at 08:12 AM
It is because of transform.translate, what transform.translate does it literally move the object a certain amount, which really messes up colliders, believe me it messed me up for a long time. For a good supplement, I started using rigidbody.velocity, like this rigidbody.velocity = transform.forward Forward Time.deltaTime; It doesn't give the exact same effect, and to stop the sliding I messed around with Drag a bit in rigid body, hope this helps
Answer by FrozenTrident · May 09, 2021 at 04:13 AM
The only Problem with Rb.velocity has a sliding effect and if you want a similar movement to translate.transform use Rb.MovePosition in the fixed update that way it will interact with the colliders and stop the sliding and you wont need to mess around with the drag which has side effects and cause you to jump weirdly;
Your answer
Follow this Question
Related Questions
Transforming position for different colliders for repeating background? 0 Answers
Colide objects with runtime gizmo movement 0 Answers
Object collider not operating properly 2 Answers
Stopping my player from moving when hitting a wall. 5 Answers
Sinking Sand 2 Answers