- Home /
stop vehicle from moving through walls
I have this simple auto acceleration ship movement script for my racing game (it's in javascript)
var movementSpeed = 80;
var rotationSpeed : float = 100.0;
function Update () {
transform.position += transform.forward * Time.deltaTime * movementSpeed;
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
transform.Rotate (0, rotation, 0);
}
but because of the way it's set up it's causing the player's ship to phase through walls even if I have a collider and rigibody attached to it, how can I fix this?
Answer by Favouriteless · Nov 12, 2018 at 01:39 PM
You shouldn't directly change the transform.position of a GameObject for movement as this may cause it to stop colliding. You should use transform.Translate or Rigidbody.AddRelativeForce and Rigidbody.AddForce for the majority of your movements.
Also not entirely sure you know what Time.deltaTime is and why it's used if you aren't doing the correct movement. Time.deltaTime is the time in seconds it took to complete the previous frame and essentially turns the script into "Move this many units per second.
The Rigidbody being Kinematic could also cause this issue with collision.
I found a better script on the forums that doesn't affect collisions so its all good. Thanks for the few pointers though man, really appreciated :)
Your answer
Follow this Question
Related Questions
Clamping rigidbody's Z position causing issues in the editor, when transform hits zero on Z axis 0 Answers
Moving a kinematic object a fixed distance over a set time with physics in mind 2 Answers
Unity Physics On Input Issue? 0 Answers
Strange Rigidbody Behavior 0 Answers
Object not moving,Object not moving in any direction 1 Answer