I have an object with a raycast coming from the front. The object will go forward and when the raycast hits a wall. It will active the turn function to avoid colliding with the wall. Once the raycast is no longer hitting the wall, the object will move forward.
But when the raycast hit a wall. The object jitters back and forth while rotating. What is happening?
var runSpeed: float;
var myCheck : boolean = true;
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;
if(myCheck){
turnProc();
}else{
goForwardProc();
}
if((Physics.Raycast(transform.position, -directionC, hit, 10)) (hit.collider.tag == “Wall”)){
myCheck = true;
}else {
myCheck = false;
}
}
function turnProc()
{
transform.Rotate(0,1, 0);
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);
}
Could it be something to do with this? LINKS!
No I doubt this has anything to do with scale. I think it has to do with checking and activation a function frame at a time… or is it second at a time? I don’t know… This problem doesn’t happen with this code…
var runSpeed: float;
var myCheck : boolean = true;
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;
if(myCheck){
turnProc();
}else{
goForwardProc();
}
if((Physics.Raycast(transform.position, -directionB, hit, 1)) (hit.collider.tag == “Wall”)){
myCheck = false;
}else if((Physics.Raycast(transform.position, -directionC, hit, 1)) (hit.collider.tag == “Wall”)){
myCheck = true;
}
}
function turnProc()
{
transform.Rotate(0,1, 0);
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);
}
Here is the project file. It has cubes bouncing around.
412040–14277–$SpaceShooterD3.7z (112 KB)
My bad yall. Obviously this should be behaving badly… I keep overlooking the obvious. I’m going to have to remove that negative value and see how it goes when I get back. ugh!
moveDirection = Vector3(0,0,40);
moveDirection = Vector3(0,0,-40);