- Home /
The question is answered, right answer was accepted
How to prevent Collider from shaking when colliding with another Collider?
Hello!
I'm creating a player controller script for my first person game, and I have a problem. When I move the player and it collides with something, it starts shaking and moves back and forth. I've read that this is because of the transform.Translate()
function, so I've tried placing it in the LateUpdate()
function instead of the default Update()
function, to make it execute at the same time as the physics code. That makes it a bit better, but it still shakes too much. It works well with the built in CharacterController.Move()
, but I want to code the controller myself, so I wonder how I can solve this. I've read one possible solution; to use multiple raycasts and check if something is blocking the player before it is being moved, but I don't really know how I can do that so that it checks "the whole area in front of the player"?
All help is appreciated!
// TheDDestroyer12
Answer by hexagonius · Jan 05, 2015 at 05:33 PM
FixedUpdate would run your code in the same step as the physics calculation. Collision and translation never is a good idea, because it bypasses the calculations of the physics engine. Instead of translating by a certain amount, set the velocity of the object (if course it needs a rigidbody). It will make the object move by the same amount, but the collision will work fine. Do that in FixedUpdate().
Check this out: http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.LateUpdate.html
It states "LateUpdate is called after all Update functions have been called"
This especially useful for camera movement, because you usually want it to move after everything else has moved. LateUpdate makes sure of that.
Okay, thanks! Now I'll move my camera rotation part of the script to the LateUpdate()
method. :)
Answer by bowserscastle · Apr 24, 2018 at 01:42 AM
To expand on the other answer with a code sample, you'd do something like this:
public Vector2 movement = new Vector2();
Rigidbody2D rb2D;
private void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
GetInput();
MoveCharacter(movement);
}
private void GetInput() {
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}
public void MoveCharacter(Vector2 movementVector)
{
movementVector.Normalize();
// move the RigidBody2D instead of moving the Transform
rb2D.velocity = movementVector * movementSpeed;
}
@bowserscastle How would I do the same thing, if it way in 3d. As in if you move your mouse left your object moves and if you press the w key it moves in the direction you are going, without the jittering when colliding with objects. Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class $$anonymous$$oveCharacter : $$anonymous$$onoBehaviour
{
public float speed;
public Rigidbody rb;
void Update()
{
float xmove = Input.GetAxis("Horizontal") * speed;
float zmove = Input.GetAxis("Vertical") * speed;
rb.velocity = new Vector3(xmove, 0, zmove);
}
}
Follow this Question
Related Questions
Object Collision PLEASE HELP!!! 1 Answer
Very simple collision death 1 Answer
Best collision detection method? 2 Answers
How do you execute Trigger-collider collision only in one gameobject? 1 Answer
Problem with Vectrosity line colliders 0 Answers