Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trouble with Physics.Raycast

Discussion in 'Scripting' started by Finkton, Apr 10, 2015.

  1. Finkton

    Finkton

    Joined:
    Apr 10, 2015
    Posts:
    4
    Hello everyone! I am new to Unity, I have built some simple scripts that work, using the documentation Unity provides; however, I can not seem to get Raycasting to work correctly. It is saying that I have invalid arguments, but I see in the documentation the arguments are all correct. I am programming with C# Thank you for the help!

    I do have the raycast script added to the First Person Camera controller.

    upload_2015-4-10_14-2-17.png
     
    Last edited: Apr 10, 2015
  2. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    You are probably looking for something like this:
    Code (csharp):
    1. void Update() {
    2.     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3.     RaycastHit hit;
    4.  
    5.     if (Physics.Raycast(ray, out hit, 100))
    6.         Debug.DrawLine(ray.origin, hit.point);
    7. }
    or this: http://docs.unity3d.com/ScriptReference/Physics.Linecast.html
     
  3. SantosR

    SantosR

    Joined:
    Dec 18, 2013
    Posts:
    27
    Looks like you are using a definition for the class "RaycastHit" that isn't Unity's implementation.

    Could you paste all your using directives?
     
  4. Finkton

    Finkton

    Joined:
    Apr 10, 2015
    Posts:
    4

    Thank you for the help, though im still getting invalid argument errors

    upload_2015-4-10_14-31-38.png
     
  5. Finkton

    Finkton

    Joined:
    Apr 10, 2015
    Posts:
    4
    Of course

    upload_2015-4-10_14-32-33.png
     
  6. SantosR

    SantosR

    Joined:
    Dec 18, 2013
    Posts:
    27
    I just noticed your class is called RaycastHit :)

    You need to change its name or specify UnityEngine.RaycastHit inside your Update() code
     
    Last edited: Apr 10, 2015
  7. Nickk888

    Nickk888

    Joined:
    Apr 10, 2015
    Posts:
    4
    Hmmmm, maybe you should use this:
    Code (CSharp):
    1. RaycastHit hit;
    2. if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out hit, 1.5f))
    3. {
    4.     if(hit.transform.gameObject.tag == "Core")
    5.     {
    6.         Instantiate(DestroyEffectObject, hit.transform.position, Quaternion.LookRotation(Vector3.forward));
    7.         Instantiate(ExplosionSound, hit.transform.position, Quaternion.LookRotation(Vector3.forward));
    8.         Destroy(hit.transform.gameObject);
    9.         GameController.GetComponent<GameManager>().CoresGet++;
    10.         CoreAmountGUITimer = CoreAmountGUITimerSet;
    11.     }
    12. }
    I use this to "grab" thinks, like the Slender game, implement this to you'r camera, just edit it for yourself.
     
  8. Finkton

    Finkton

    Joined:
    Apr 10, 2015
    Posts:
    4
    Great! It works, why did I have to mention the UnityEngine class specifically?
     
  9. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,834
    You have two classes with same names. You can uniquely distinguish only by name space. Here UnityEngine is the namespace for RaycastHit Unity's class . So you need to mention UnityEngine.RaycastHit