- Home /
detect collider with button movement
Hi there I have 2 control systems in my project. One the general mouse look and keyboard movement. The other uses buttons to control all movement. Unfortunately the buttons override the ability to collide and stop at walls/doors etc. I thought of using raytracing but I'm unsure whether this would stop you from walking sideways through walls. Heres the code i'm using any advice would be great.
var speed : float = Time.deltaTime * 100;
if (movement==1){
transform.Translate(0,0,speed);
fly =0;
}
if (movement==2){
transform.Translate(0,0,-speed);
fly =0;
}
if (movement==3){
transform.Translate(-speed,0,0);
fly =0;
}
if (movement==4){
transform.Translate(speed,0,0);
fly =0;
}
Answer by mattssonon · Oct 25, 2013 at 10:37 AM
When you use .Translate()
you're most likely just moving the character to the other side of the collider, .e.g it's like teleporting not moving. You should use a different method, e.g. if it's a CharacterController
, use .Move()
or .SimpleMove()
.
thank you matt that works!!! Is there anyway of grounding the object? when i use a rigid body it still falls through
Great. Which object do you want to be grounded? What does it fall through? Feel free to accept the answer if it helped, so the question is categorized as solved for other visitors.
hi matt new problem has arisen. This script works for moving but it only moves along the axis not the way your facing.
if (movement==2){
transform.GetComponent(CharacterController).$$anonymous$$ove(Vector3.forward*speed);}
Heres attempts but they make no difference
if (movement==6){
transform.Rotate(0,speed,0);}
if (movement==5){
transform.Rotate(0, Input.GetAxis ("Horizontal") * speed, 0);}
thanks for all your help
Try using transform.forward
ins$$anonymous$$d of Vector3.forward
, since Vector3.forward
is just a vector pointing in the Z direction, which is not always the direction you're facing.
fantastic i assumed it was my rotation method that was off but it was the translation moving along the world axis thanks so much heres some of the working code for anyone with this problem in future.
var speed : float = Time.deltaTime * 100;//adjust the figure according to the scale of your scene//
if (movement==1){//forward button//
transform.GetComponent(CharacterController).$$anonymous$$ove(transform.forward*speed);
}
if (movement==5){//rotate left//
transform.Rotate(0,-speed,0);
}