Search Unity

Raycasting only hits item sometimes

Discussion in 'Scripting' started by Luke-L, Feb 18, 2020.

  1. Luke-L

    Luke-L

    Joined:
    Feb 18, 2020
    Posts:
    3
    I've been struggling trying to get raycasting to work all morning and I'm at the point of total frustration. Pretty much I need a raycast to hit an object and if the User hits E, it should send a Message to execute the Press() function in that object if the object has the correct tag.

    This is my SelectObjects script, it has a cylinder that is attached to the Main Camera
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SelectObjects : MonoBehaviour
    6. {
    7.     [SerializeField] private string selectableTag = "Selectable";
    8.     public Transform forw;
    9.     private bool pressedRecently;
    10.     private float timepassed;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         CheckPressedRecently();
    21.         if (!pressedRecently)
    22.         {
    23.             RayCastStuff();
    24.         }
    25.        
    26.     }
    27.  
    28.     void RayCastStuff()
    29.     {
    30.         Transform cameraTransform = Camera.main.transform;
    31.         Ray forwardRay = new Ray(forw.position, forw.forward);
    32.         Debug.Log(forw.position + " " + forw.forward);
    33.         RaycastHit[] hits=Physics.RaycastAll(forwardRay);
    34.         for (int i = 0; i < hits.Length; i++)
    35.         {
    36.             if (hits[i].collider.CompareTag(selectableTag))
    37.             {
    38.                 if (Input.GetKey(KeyCode.E))
    39.                 {
    40.                     Debug.Log("E pressed");
    41.                     hits[i].collider.SendMessage("Press");
    42.                 }
    43.             }
    44.         }
    45.         }
    46.     void CheckPressedRecently()
    47.     {
    48.         timepassed += Time.deltaTime;
    49.         if(timepassed > .25f)
    50.         {
    51.             timepassed = 0;
    52.             pressedRecently = false;
    53.         }
    54.         else
    55.         {
    56.             pressedRecently = true;
    57.         }
    58.     }
    59. }
    60.  
    My Camera movement is as follows( I figured this might help)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerController : MonoBehaviour
    6. {
    7.     public Camera cam;
    8.     public float camSpeed;
    9.     public float playerSpeed;
    10.     private float yaw;
    11.     private float pitch;
    12.     float distance = 5;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.        
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         CheckCamera();
    23.         Rigidbody rb = GetComponent<Rigidbody>();
    24.         float y = transform.position.y;
    25.         if (Input.GetKey(KeyCode.A))
    26.             transform.position = transform.position + -Camera.main.transform.right * distance * Time.deltaTime;
    27.         else if (Input.GetKey(KeyCode.D))
    28.             transform.position = transform.position + Camera.main.transform.right * distance * Time.deltaTime;
    29.         else if (Input.GetKey(KeyCode.W))
    30.             transform.position = transform.position + Camera.main.transform.forward * distance * Time.deltaTime;
    31.         else if (Input.GetKey(KeyCode.S))
    32.             transform.position = transform.position + -Camera.main.transform.forward * distance * Time.deltaTime;
    33.         transform.position = new Vector3(transform.position.x, y, transform.position.z);
    34.     }
    35.  
    36.     void CheckCamera()
    37.     {
    38.         yaw += camSpeed * Input.GetAxis("Mouse X");
    39.         pitch += camSpeed * Input.GetAxis("Mouse Y");
    40.         if (pitch > 50)
    41.             pitch = 50;
    42.         else if (pitch < -100)
    43.             pitch = -100;
    44.         transform.eulerAngles = new Vector3(-pitch, yaw, 0.0f);
    45.     }
    46.  
    47. }
    48.  

    The cylinder is on the Main Camera with the following info:
    Position: 0,0,2.72
    Scale(0.05,2,0.05)

    Its supposed to interact with objects that have the tag. But it only does so sometimes. I have a box collider, and it will only work with that one if it's close and aimed towards the top of the box (above the collider it looks like). It wont hit any of my smaller colliders.

    Here is an example of an object it should hit, but it doesn't.
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    use raycast show line for help
     
  3. Luke-L

    Luke-L

    Joined:
    Feb 18, 2020
    Posts:
    3
    Are you talking about Debug.DrawRay()?
     
  4. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    am not sure but it seems issue with different size object so maybe first port of call is to visualise the raycast line?
     
  5. Luke-L

    Luke-L

    Joined:
    Feb 18, 2020
    Posts:
    3
    Thanks, Visualizing helped. I realized I needed to use .up not .forward!
     
  6. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    no problem