- Home /
my character is moving through walls
My character is moving through walls with the new script I am using to move in world axis. I was wondering if someone could help me so that it dosent go through walls.
var fixedDirection:Transform; //drag 'n drop the "Direction" game object to this slot.
var speed:float = 1.0; //Change to whatever you want.
function Update(){
transform.Translate(fixedDirection.forward * Input.GetAxis("Vertical") * Time.deltaTime * speed);
transform.Translate(fixedDirection.right * Input.GetAxis("Horizontal") * Time.deltaTime * speed);
}
i dont understand why it is doing it.
any help would be appritiated.
where-abouts are you detecting collisions with the walls?
Answer by Umbra · Oct 25, 2011 at 08:25 PM
When you simply adjust the transform like that, it doesn't care about collision control. Here's how I would do it:
var controller:CharacterController = GetComponent(CharacterController)
var MoveDirection:Vector3 = Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical");
MoveDirection = transform.TransformDirection(MoveDirection);
MoveDirection *= speed;
controller.Move(MoveDirection * Time.deltaTime);
The Character Controller must be attached to your character (Component->Physics->CharacterController). For a better version of this script, look up the documentation for CharacterController.Move().
Answer by easy_da_man · Oct 25, 2011 at 08:25 PM
Not sure if you have already done that!? But try attaching a RigidBody and a Collider to the game object in question. And you might check the objects your "controllable" game object collides with. They need at least RigidBody attached... maybe also a Collider. Read that up in the unity3d api reference: http://unity3d.com/support/documentation/Components/class-Rigidbody.html
You will find the entry for the Collider yourself :-)
Peace and good luck
for some reason as soon as i add a rigid body my character falls though the floor. this is very odd.
Answer by PaxNemesis · Oct 25, 2011 at 08:18 PM
Check out CharacterController.SimpleMove should be what you need. :=)
unfortunatly this is differnt from what i need as i need it to not rotate the camera when turning, i just need it to move the object in the direction of the movement key being pressed. thanks anyway.