- Home /
Rotate with Raycast???
how can i get this to rotate when the raycast is no longer hitting the collider. This is for AI i want it to rotate when it hits the collider then move when it is no longer colliding been on with this for about 7 hours someone please help:(
 function Update()
 
 {    
 
 if(canMove == true)
     {
         if(distance <= rangeToAttack)
     
         {
             var forward : Vector3 = transform.TransformDirection(Vector3.forward);
             var curSpeed : float = speed * Time.deltaTime;
             robotController.SimpleMove(forward * curSpeed);
             animation.CrossFade("walk");
             transform.LookAt(player);
             transform.eulerAngles.x = 0;
                 if(distance < 15)
                 {
                     animation.CrossFade("attack");
                     animation["attack"].speed = 0.3f;
                 }
                 
         }
     }
     
     var dir = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     Debug.DrawRay(transform.position, dir * rayLength, Color.blue);
     
     if (Physics.Raycast(transform.position, dir, hit, rayLength))
         {
             
             if(hit.collider.gameObject.tag == "Prop")
                 {
                     canMove = false;
                     transform.Rotate(0,rotateAmount,0 * Time.smoothDeltaTime);
                 }
                 else if(hit.collider = false)
                 {
                     canMove = true;
                 }
                 
             
         }
 
     
     
     
     
 }
0 * Time.smoothDeltaTime isn't doing anyone any good. Otherwise, is the problem that it should continue to rotate for a little after getting clear?
yea it should rotate until the raycast is not hitting anything anymore
Answer by monkeyThunk · Nov 24, 2011 at 09:37 PM
 if (Physics.Raycast(transform.position, dir, hit, rayLength))
Raycast returns true if it hit something, false if it doesn't
       else if(hit.collider = false)
this line makes no sense, hit.collider is the collider that the Raycast hit, you use it above correctly (hit.collider.gameObject.tag == "Prop") but here you are actually trying to assign false to it, I think this will always evaluate to false, and you'll never set canMove to true.
I think this is what you want:
if (Physics.Raycast(transform.position, dir, hit, rayLength)) { // rotate } else { // move and maybe attack } if (hit) rotate else move and maybe attack
Answer by aldonaletto · Nov 24, 2011 at 10:07 AM
There are some errors in the rotation part: as @OwenReynolds said, the 3rd parameter makes no sense - you should actually multiply the 2nd parameter by Time.deltaTime to make the AI rotate smoothly; another error: the instruction if (hit.collider = false) is wrong - "=" is the assignment operator, and collider isn't boolean anyway; you should use if (!hit.collider) or if (hit.collider==null) to do something when the collider doesn't exist.
 That's the fixed code:
...
if (Physics.Raycast(transform.position, dir, hit, rayLength)){
    if (hit.gameObject.tag == "Prop"){
         canMove = false;
         transform.Rotate(0,rotateAmount*Time.deltaTime,0);
    }
    else if (!hit.collider){
         canMove = true;
    }
}
 NOTE: This is a syntax fix; I didn't check the logic. 
              Your answer
 
 
             Follow this Question
Related Questions
Raycasting crashing Unity? 3 Answers
Modifying AI script to follow gameobject 1 Answer
Linecast is always blocked. 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                