Skip to content

Instantly share code, notes, and snippets.

@tracend
Created March 30, 2011 00:20
Show Gist options
  • Save tracend/893632 to your computer and use it in GitHub Desktop.
Save tracend/893632 to your computer and use it in GitHub Desktop.
Unity3D: Raycast object avoidance - Source: http://wiki.dreamsteep.com/Unity_ai
var target : Transform;
function Update(){
//the direction vector to target
var dir = (target.position - transform.position).normalized;
var hit :RaycastHit;
if (Physics.Raycast(transform.position,transform.forward,hit ,20)){
//no self collision
if(hit.transform !=transform)
{
dir += hit.normal *20;
}
}
var rot = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation,rot,Time.deltaTime);
transform.position += transform.forward *20 *Time.deltaTime;
print (transform.rotation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment