unity 3d student

Intermediate I01 – Raycasting


How to detecting collisions using Rays by raycasting from objects in 3D space.

Code Used (Javascript)

function Update () {
  1.  
  2.    var up = transform.TransformDirection(Vector3.up);
  3.    var hit : RaycastHit;    
  4.    Debug.DrawRay(transform.position, -up * 10, Color.green);
  5.  
  6.    if(Physics.Raycast(transform.position, -up, hit, 10)){
  7.       Debug.Log("Hit");    
  8.       if(hit.collider.gameObject.name == "floor"){
  9.            Destroy(GetComponent(Rigidbody));
  10.       }
  11.    }
  12. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

61 Comments