I’m trying to make a cube move in a direction when a raycast registers a hit with a wall. It seems to work. But the raycast in the scene view is falling off the cube then bouncing back. And it continues to fall further away while bouncing back and forth. It happens even when the raycast doesn’t register a hit.
Any help would be much appreciated. Thank you.
var runSpeed: float;
private var moveDirection = Vector3.zero;
var controller :CharacterController;
function Awake()
{
controller = GetComponent(CharacterController);
}
function Update () {
var directionA = transform.TransformDirection(Vector3(1,0,0));
var directionB = transform.TransformDirection(Vector3(0,0,-1));
var directionC = transform.TransformDirection(Vector3(0,0,1));
var hit : RaycastHit;
Debug.DrawRay(transform.position, -directionA * 10, Color.blue);
Debug.DrawRay(transform.position, -directionB * 10, Color.green);
Debug.DrawRay(transform.position, -directionC * 10, Color.green);
if(Physics.Raycast(transform.position, -directionA, hit, 10))
{
if(hit.collider.tag == “Wall”)
{
Debug.Log(“Hit”);
turnProc();
}
else
{
goForwardProc();
}
}
}
function turnProc()
{
moveDirection = Vector3(0,0,40);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= runSpeed;
controller.SimpleMove(moveDirection * Time.deltaTime);
}
function goForwardProc()
{
moveDirection = Vector3(0,0,40);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= runSpeed;
controller.SimpleMove(moveDirection * Time.deltaTime);
}
[edit; nm, completely mis-read the question]
Okay I fixed it… Apparently having a character controller and a rigidbody on the same object causes bad stuff to happen.