Search Unity

[SOLVED] Raycast not detecting object's variable correctly

Discussion in 'Physics' started by salmonmondays, May 23, 2021.

  1. salmonmondays

    salmonmondays

    Joined:
    Nov 12, 2020
    Posts:
    17
    Hey everyone,
    I am making a game where you can control and redirect objects that are moving, and I am running into a problem with my player raycasting. It is applying the controlling script to an object that is not moving and the movement detection script (isMoving) is not detecting movement from the object I apply the script to. Any ideas why the deleteafterTest isn't being detected correctly? Also, when I use the objectisMoving variable from my movement detection script instead of the deleteafterTest variable, it is not detecting the bool correctly. Here is the code, and I apologize for how messy it is right now:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Raycast : MonoBehaviour
    7. {
    8.     public GameObject playerCamera;
    9.     public float distance;
    10.     public float camPositionFloat;
    11.     public bool isParented;
    12.     public FirstPersonMovement playerMovement;
    13.     public FirstPersonLook playerLook;
    14.     public Transform cameraPosition;
    15.     public Jump playerJumping;
    16.     public Crouch playerCrouching;
    17.     public GameObject targetHit;
    18.     public Controll_Raycasted_Object controllObjectScript;
    19.     public Rigidbody targetRigidbody;
    20.     public TimeManager theTimeManager;
    21.     public float timeStamp;
    22.     public float coolDownPeriodInSeconds = 2f;
    23.     public isMoving ismovingScript;
    24.     public float raycastStep;
    25.     public bool objectIsMovingTest;
    26.  
    27.  
    28.     void Start()
    29.     {
    30.         isParented = false;
    31.         objectIsMovingTest = false;
    32.         //timeStamp = Time.time + coolDownPeriodInSeconds;
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         if (Input.GetMouseButtonDown(0))
    38.         {
    39.             if (isParented == false)
    40.             {
    41.                 // Raycasting the object to send the camera to
    42.                 RaycastHit hit;
    43.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    44.             raycastStep = 1;
    45.             if (Physics.Raycast(ray, out hit, 1000))
    46.             {
    47.                 if (hit.distance < 15)
    48.                 {
    49.                     if (raycastStep == 1)
    50.                     {
    51.                     // //Adding or removing movement testing script (isMoving)
    52.                     // if (hit.collider.GetComponent<isMoving>() != null)
    53.                     // {
    54.                     //     Destroy (hit.collider.gameObject.GetComponent<isMoving>());
    55.                     //     Debug.Log("destroy component");
    56.                     //     raycastStep = 2;
    57.                     // }
    58.  
    59.                         if (hit.collider.GetComponent<isMoving>() == null)
    60.                         {
    61.                             hit.collider.gameObject.AddComponent<isMoving>();
    62.                             raycastStep = 2;
    63.                         }
    64.                     }
    65.                     // Controlling the gameobject that is moving
    66.  
    67.  
    68.                     if (raycastStep == 2)
    69.                     {
    70.                         if (hit.collider.gameObject.GetComponent<isMoving>() == true)
    71.                         {
    72.                             targetHit = hit.collider.gameObject;
    73.                             ismovingScript = hit.collider.gameObject.GetComponent<isMoving>();
    74.                             if (targetHit.GetComponent<isMoving>().deleteafterTest == 1)
    75.                             {
    76.                                 hit.collider.gameObject.AddComponent<Controll_Raycasted_Object>();
    77.                                 targetRigidbody = targetHit.GetComponent<Rigidbody>();
    78.                                 // Debug the length to the gameobject that has been raycasted;
    79.                                 Debug.Log(hit.collider.gameObject.name + " is " + Mathf.Round(distance = hit.distance) + " feet away");
    80.                                 // Setting the parent bool to true
    81.                                 isParented = true;
    82.                                 // Disable Player GameObject Looking and Movement
    83.                                 playerMovement.enabled = false;
    84.                                 playerLook.enabled = false;
    85.                                 playerJumping.enabled = false;
    86.                                 playerCrouching.enabled = false;
    87.                             }
    88.                         }
    89.                         else
    90.                         {
    91.                             Debug.Log("Object not moving");
    92.                         }
    93.                     }
    94.                 }
    95.                 else
    96.                 {
    97.                     Debug.Log("Nothing was detected");
    98.                 }
    99.             }
    100.             }
    101.             else
    102.             {
    103.                 playerMovement.enabled = true;
    104.                 playerLook.enabled = true;
    105.                 isParented = false;
    106.                 playerCrouching.enabled = true;
    107.                 playerJumping.enabled = true;
    108.                 Destroy(targetHit.GetComponent<isMoving>());
    109.                 Destroy(targetHit.GetComponent<Controll_Raycasted_Object>());
    110.             }
    111.         }
    112.         if (Input.GetMouseButtonDown(1))
    113.         {
    114.             //if (timeStamp <= Time.time)
    115.             //{
    116.                 theTimeManager.DoSlowMotion();
    117.            // }
    118.         }
    119.     }
    120.     void FixedUpdate()
    121.     {
    122.         if (isParented == true)
    123.         {
    124.             playerCamera.transform.position = targetHit.transform.position;
    125.             playerCamera.transform.rotation = targetHit.transform.rotation;
    126.         }
    127.         if (isParented == false)
    128.         {
    129.             playerCamera.transform.position = cameraPosition.gameObject.transform.position;
    130.             playerCamera.transform.rotation = cameraPosition.transform.rotation;
    131.         }
    132.     }
    133. }
    134.  
    Here is my movement detection code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class isMoving : MonoBehaviour
    6. {
    7.     public Vector3 lastPosition;
    8.     public Transform myTransform;
    9.     public bool objectIsMoving = false;
    10.     public float deleteafterTest = 1;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         myTransform = transform;
    15.         lastPosition = myTransform.position;
    16.     }
    17.     void Update()
    18.     {
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void FixedUpdate()
    24.     {
    25.         if (myTransform.position != lastPosition)
    26.         {
    27.             objectIsMoving = true;
    28.             deleteafterTest = 2;
    29.         }
    30.         else
    31.         {
    32.             objectIsMoving = false;
    33.             deleteafterTest = 1;
    34.         }
    35.         lastPosition = myTransform.position;
    36.     }
    37. }
    38.  
    And finally, here is my code I am applying to control the raycasted object (may not be needed by you guys):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Controll_Raycasted_Object : MonoBehaviour
    6. {
    7.     public Raycast raycastingScript;
    8.     public Rigidbody objectControllerRigidbody;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.    
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         objectControllerRigidbody = this.GetComponent<Rigidbody>();
    19.         raycastingScript = FindObjectOfType<Raycast>();
    20.     }
    21.     void FixedUpdate()
    22.     {
    23.         if (raycastingScript.isParented == true)
    24.         {
    25.             this.transform.Translate(0f, 0f, 2f * Time.deltaTime);
    26.             // originally 25f above
    27.             objectControllerRigidbody.transform.Rotate(-1250 * Input.GetAxis("Vertical") * Time.deltaTime, 0, 0);
    28.             //originally -1000 and 500 below
    29.             objectControllerRigidbody.transform.Rotate(0, 625 * Input.GetAxis("Horizontal") * Time.deltaTime, 0);
    30.         }
    31.     }
     
    Last edited: May 24, 2021
  2. salmonmondays

    salmonmondays

    Joined:
    Nov 12, 2020
    Posts:
    17
    I found the error to my "Raycast" script and here is the answer for others that run into this problem as well:
    In the raycaststep 2 I used
    if (targetHit.GetComponent<isMoving>().deleteafterTest == 1)
    , and this was not detecting the variables correctly from my "isMoving" script. Here is what I did to fix it, and it is embarrassingly simple:
    if (hit.collider.gameObject.GetComponent<isMoving>().deleteafterTest == 2)

    I hope this helps with anyone else encountering this problem:)
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    It's a minor thing but note that you can get components on a GameObject by calling GetComiponent on any component on that GameObject so above you can omit the ".gameObject" part and have "
    if (hit.collider.GetComponent".

    Here's the API docs for Collider2D (see the inherited GetComponent methods).
     
  4. salmonmondays

    salmonmondays

    Joined:
    Nov 12, 2020
    Posts:
    17
    Thanks, that helps a lot