Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Problem with rotation Object raycast

Discussion in 'Scripting' started by MedalHellWay, Jul 29, 2020.

  1. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    131
    Hi everyone again!

    I have a strange rotation problem with raycast. (If you want, to examine the error, I have attached a very small video of the problem).
    Basically depending on where I aim with the raycast the object rotates or not! I can't understand why or if I'm wrong. I've a collider attach to object that works with the filtered raycast. I repeat everything works, but according to the point where it hits the beam


    I use two scripts: One for the object to rotate, and another for the Player interact. With the player

    Object Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityStandardAssets.Characters.FirstPerson;
    5.  
    6. public class RotateObject : MonoBehaviour
    7. {
    8.     public GameObject obj;
    9.     [Space]
    10.     public float rotSpeed = 100f;
    11.     public float distanceFromCamera = .8f;
    12.     [Space]
    13.     public GameObject player;
    14.  
    15.     private Vector3 objPos;
    16.     private Quaternion objRot;
    17.  
    18.     private Vector3 nv;
    19.  
    20.     private float rotX;
    21.     private float rotY;
    22.  
    23.     //Camera
    24.     private Camera m_MainCamera;
    25.     private Vector3 CenterCamera;
    26.  
    27.     private void Awake()
    28.     {
    29.         Vector3 eulerAngles = transform.eulerAngles;
    30.         rotX = eulerAngles.x;
    31.         rotY = eulerAngles.y;
    32.     }
    33.  
    34.     private void Start()
    35.     {
    36.         obj = this.gameObject;
    37.  
    38.         //store position and rotation of object
    39.         objPos = obj.transform.position;
    40.         objRot = obj.transform.rotation;
    41.  
    42.         m_MainCamera = Camera.main;
    43.     }
    44.  
    45.     public void CC()
    46.     {
    47.         player.GetComponent<FirstPersonController>().enabled = false;
    48.         CenterCamera = m_MainCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, distanceFromCamera));
    49.         obj.transform.position = CenterCamera;
    50.     }
    51.  
    52.  
    53.     public void OnMouseDrag()
    54.     {
    55.         rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
    56.         rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
    57.  
    58.         obj.transform.Rotate(Vector3.up, -rotX, Space.World);
    59.         obj.transform.Rotate(Vector3.right, rotY, Space.World);
    60.         obj.transform.Translate(rotSpeed * nv.normalized * Time.deltaTime);
    61.     }
    62.  
    63.     public void ResetObj()
    64.     {
    65.         obj.transform.position = objPos;
    66.         obj.transform.rotation = objRot;
    67.     }
    68.  
    69. }
    Part of Player script:

    Code (CSharp):
    1. Ray ray = new Ray(transform.position, transform.forward);
    2.         RaycastHit hit;
    3.  
    4.         if (Physics.Raycast(ray, out hit, InteractDistance, maskLayer))
    5.         {
    6.             Debug.DrawLine(ray.origin, hit.point, Color.red);
    7.  
    8.             if (checkImpact == false)
    9.             {
    10.                 if (crosshair != null && hit.collider.CompareTag("InteractableObj"))
    11.                 {
    12.                     crosshair.enabled = true;
    13.                 }
    14.             }
    15.  
    16.             if (Input.GetMouseButtonDown(0) && clickLeft)
    17.             {
    18.                 ///--- MORE CODE HERE --- ///
    19.  
    20.                     if (hit.collider.gameObject.name == "painting")
    21.                     {
    22.                         isClickRightActive = true;
    23.                     }
    24.  
    25.                     if (isClickRightActive)
    26.                     {
    27.                         //reference for script RotateObject
    28.  
    29.                         hit.collider.GetComponent<RotateObject>().CC();
    30.                         hit.collider.GetComponent<RotateObject>().OnMouseDrag();
    31.                     }
    32.                 }
    33.             }
    34.         }
    35.         else crosshair.enabled = false;
    36.  
    37.         if (Input.GetMouseButtonDown(1) && isClickRightActive)
    38.         {
    39.             hit.collider.GetComponent<RotateObject>().ResetObj();
    40.             player.GetComponent<FirstPersonController>().enabled = true;
    41.             isClickRightActive = false;
    42.         }
    43.     }
    Where do you think I'm wrong? Thanks for any help :)
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    8,471
    You might want to use Debug.DrawRay() or Debug.DrawLine() to visualize the ray in realtime, and of course Debug.Log() to print out values such as how much it is rotating, is the code even running, etc.

    Since your rotate commands (line 58 and 59) only rotate around Y and X axes, if a ray is predominantly pointed down the Z axis, it won't really rotate visually. Is that what's happening?
     
    MedalHellWay likes this.
  3. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    131
    Thanks for reply Kurt.
    I use the
    Code (CSharp):
    1. Debug.DrawLine(ray.origin, hit.point, Color.red);
    to visualize the radius and everything works fine.
    Actually with this specific object I noticed that the central part does the "broken rotation" job and therefore not on a particular axis. If you look the video attach in the discussion, you can view the rotation behavior ...
     
  4. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    131
    I have done many things, but I still have this strange problem ... :(
     
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,130
    The DrawRay isn't quite right. The second input isn't a point -- it's an offset from the start. You'd need to use DrawRay(ray.origin, hit.point-ray.origin). Since you're giving it a Vector3, there's no error, but it's not showing you where the ray actually goes.

    Note that DrawRay _almost_ works like raycast. Raycast takes a direction and a distance. It obviously ignores how long the direction is (otherwise what was distance for?) DrawRay takes only a "direction offset" and goes that exact distance.

    I'd also separate out the rotation idea. You've got a 2-step process. Raycasts just hit where they hit. They can't push or move or do anything. Either it hits where it should, or it doesn't. The next step is using that to rotate. Rotation could be wrong since the hit.point is wrong, or for some other reason.
     
    MedalHellWay likes this.
  6. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    131
    Thanks for reply and tip Owen :)
     
unityunity