Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycast problem

Discussion in 'Scripting' started by half_voxel, Mar 28, 2008.

  1. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Hi.

    When i raycast in my game i have an object to look at the point the ray hits the collider but it just looks at the middle of the gameObject it is hitting, what am i doing wrong??


    Code (csharp):
    1.  
    2. function Update () {
    3.     var dir = camera_orbit.transform.TransformDirection (Vector3.forward);
    4.     dir.y = 0;
    5.     var hit : RaycastHit;
    6.     var rotation : Quaternion;
    7.     if (Physics.Raycast(transform.position + dir,dir,50)) {
    8.         if (wepon == 1) {
    9.             rotation = Quaternion.LookRotation(hit.point - cannon1.transform.position);
    10.             cannon1.transform.rotation = Quaternion.Slerp(cannon1.transform.rotation, rotation, Time.deltaTime * damping);
    11.             rotation = Quaternion.LookRotation(hit.point - cannon2.transform.position);
    12.             cannon2.transform.rotation = Quaternion.Slerp(cannon2.transform.rotation, rotation, Time.deltaTime * damping);
    13.             if (Input.GetAxis ("Fire")  fire) {
    14.                 if (wcannon == 1) {
    15.                     can1wepon1fire ();
    16.                 } else {
    17.                     can2wepon1fire ();
    18.                 }
    19.             }
    20.         }
    21.     }
    22. }
    23.  
     
  2. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    mm no it actually returns zero?!?!
     
  3. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    changed code (but now it hangs)
    changed hit info to HIT line 6

    Code (csharp):
    1.  
    2. function Update () {
    3.     var dir = camera_orbit.transform.TransformDirection (Vector3.forward);
    4.     dir.y = 0;
    5.     var hit : RaycastHit;
    6.     var rotation : Quaternion;
    7.     if (Physics.Raycast(transform.position + dir,dir,hit,50)) {
    8.         if (wepon == 1) {
    9.             print (hit.point);
    10.             rotation = Quaternion.LookRotation(hit.point - cannon1.transform.position);
    11.             cannon1.transform.rotation = Quaternion.Slerp(cannon1.transform.rotation, rotation, Time.deltaTime * damping);
    12.             rotation = Quaternion.LookRotation(hit.point - cannon2.transform.position);
    13.             cannon2.transform.rotation = Quaternion.Slerp(cannon2.transform.rotation, rotation, Time.deltaTime * damping);
    14.             if (Input.GetAxis ("Fire")  fire) {
    15.                 if (wcannon == 1) {
    16.                     can1wepon1fire ();
    17.                 } else {
    18.                     can2wepon1fire ();
    19.                 }
    20.             }
    21.         }
    22.     }
    23. }
     
  4. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    sorry it was no problem i had just not read the docs well and the hanging was an one time accident.