Search Unity

Need Help Raycasting From Game Objects

Discussion in 'Scripting' started by JD_Designer, Jul 22, 2016.

  1. JD_Designer

    JD_Designer

    Joined:
    Sep 1, 2015
    Posts:
    111
    Hello!

    I'm trying to instantiate an object (object B) on the surface of a plane if another game object (object A) is pointed at that object. I then want object B's transform to mirror the RaycastHit.point as object A translates and thus moves the ray it's casting.

    Here is my working script:
    Code (CSharp):
    1.  
    2.     public GameObject crossHairs;
    3.     bool crossHairsSpawned;
    4.  
    5.     void Update ()
    6.     {
    7.         RaycastHit hit;
    8.         Vector3 targetPosition = RaycastHit.point;
    9.  
    10.         Vector3 forward = transform.TransformDirection (Vector3.forward) * 10;
    11.         Debug.DrawRay (transform.position, forward, Color.green);
    12.  
    13.         if (Physics.Raycast (transform.position, (forward), out hit))
    14.         {
    15.             if (hit.collider.gameObject.tag == "TargetPlane" && crossHairsSpawned == false)
    16.             {
    17.                 crossHairsSpawned = true;
    18.                 Instantiate (crossHairs, targetPosition, Quaternion.Euler (0, 0, 0));
    19.             }
    20.  
    21.             if (crossHairsSpawned == true)
    22.             {
    23.                 crossHairs.transform.position = targetPosition;
    24.             }
    25.         }
    26.     }
    However Unity is throwing: error CS0120: An object reference is required to access non-static member `UnityEngine.RaycastHit.point'

    I looked at the manual and it says to use: Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    I'm not sure this makes sense in my case since the ray is not being cast from the screen but from a game object? How can I make this work. Thanks in advance! :)
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    That's completely unrelated
    This is not the code with the error in it, find that one, that has hit.point in it
     
    JD_Designer likes this.
  3. JD_Designer

    JD_Designer

    Joined:
    Sep 1, 2015
    Posts:
    111
    Thanks. The error message is gone but I'm still fighting this. Here is the new working code:
    Code (CSharp):
    1. public GameObject crossHair;
    2.     public bool crossHairSpawned;
    3.  
    4.     void Update ()
    5.     {
    6.         RaycastHit hit;
    7.  
    8.         Vector3 forward = transform.TransformDirection (Vector3.forward) * 10;
    9.         Debug.DrawRay (transform.position, forward, Color.green);
    10.  
    11.         if (Physics.Raycast (transform.position, (forward), out hit))
    12.         {
    13.             if (hit.collider.gameObject.tag == "TargetPlane" && crossHairSpawned == false)
    14.             {
    15.                 crossHairSpawned = true;
    16.                 Instantiate (crossHair, hit.point, Quaternion.Euler (90f, 0, 0));
    17.             }
    18.  
    19.             if (crossHairSpawned == true)
    20.             {
    21.                 crossHair.transform.position = hit.point;
    22.             }
    23.         }
    24.     }
    It's not working as I'd hoped. On line 13 I have a condition about the colliding objects tag name, but at runtime it won't instantiate my prefab unless I remove that condition. Also, I need line 21 to update the transform constantly so to move my crosshair allong with the collision point of the raycast and other collider. What am I doing wrong?

    Edit: Got the CrossHair to follow the Raycast hit point by doing this:
    Code (CSharp):
    1. if (crossHairSpawned == true)
    2.         {
    3.             Physics.Raycast (transform.position, (forward), out hit);
    4.             crossHairTransform = hit.point;
    5.             GameObject.Find ("CrossHair(Clone)").transform.position = crossHairTransform;
    6.         }
    But I'm still trying to grok why my tag condition is problematic.

    Edit: I see that I'm going to have to use layer masks and not tags.
     
    Last edited: Jul 23, 2016
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Make sure your tag:
    1. actually exists
    2. is written exactly like that
    3. is assigned to the gameObject
     
  5. JD_Designer

    JD_Designer

    Joined:
    Sep 1, 2015
    Posts:
    111
    Yes 1-3 checked out but it turned out that layers and/or tags were not what I needed to be working with.

    I needed the raycast line to pass through all but one object in the scene. I solved this by checking "Ignore Raycasting" on the relevant objects, though I would have preferred to do it through code somehow. In any case, problem solved.
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350