Stop character from moving when it collides with an object
Hi, so when my character hits a wall, it stops him but he acts all jittery because he is still trying to get through. How do I stop it? The movement is automatic. Thanks for all the help!
My code:
[SerializeField] float Speed = 3.5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update() {
Invoke("RotateAndMovement", 2f);
}
void RotateAndMovement()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.eulerAngles = new Vector3(0, -90, 0);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
transform.eulerAngles = new Vector3(0, 90, 0);
}
transform.Translate(Vector3.forward * Speed * Time.deltaTime);
}
}
Answer by black_sheep · Feb 21, 2018 at 03:29 PM
You're changing the position manually without checking for collisions. So when you move it, it gets inside na object, than Unity realizes it and changes the position. Try putting a rigidbody on the character, mark it as kinematic and, if you're using FPS view, put "interpolate".
Hi, thanks for the reply. When I marked it as $$anonymous$$inematic, it just goes through the object ins$$anonymous$$d of stopping it.
Follow this Question
Related Questions
Rigid body goes flying on colliding 0 Answers
How i stop Player rotation when collide 2 Answers
Collider or Rigidbody not working 0 Answers
Need help with high speed collisions, sanity dwindling 1 Answer
Rigidbody bouncing off other rigidbodies 0 Answers