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

Raycast seams to be not fast enough while moving objects

Discussion in 'Physics' started by JoeKee, May 22, 2020.

  1. JoeKee

    JoeKee

    Joined:
    Feb 17, 2017
    Posts:
    3
    Hi all,

    in a simple experiment I saw the following: Raycast seams not fast enough while object moves.

    An object (GameObject) holds two objects (Cube, Sphere) inside (s. pic.). The Sphere holds also the camera and is only there to show the phenomen more clearly.

    The object "GameObject" has two scripts
    One to handle the raycast. It it the script from the Unity-docs:

    RayCastTest.cs
    Code (CSharp):
    1.  void Update()
    2.     {
    3.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.         RaycastHit hit;
    5.  
    6.         if (Physics.Raycast(ray, out hit, 100))
    7.             Debug.DrawLine(ray.origin, hit.point);
    8.  
    9.         return;
    10.     }
    The second one AutoMoveAndRotate.cs (s.b.) let "GameObject" move in the scene and cames from the Unity.StandardAssets. I set the x component of moveUnitsPerSecond to 10, everything else is zero.


    What is the phenomen?

    If GameObject is not moving the Raycast is exact.
    If GameObject is moving (e.g. 10 units in x-direction) the raycast seams to be not fast enough?! You can see this in the picture below. The Debug.DrawLine Should be in the middle of the objects but is moved to the left while GameObject is moving to the right.

    Question:
    What is wrong in the script/handling/setup or is this behaviour right? And if its right, how can I detect objects (and the hitPoint) under the mouse exact while these object is moving?

    Thank you for your help.
    Joe



    AutoMoveAndRotate.cs
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. namespace UnityStandardAssets.Utility
    5. {
    6.     public class AutoMoveAndRotate : MonoBehaviour
    7.     {
    8.         public Vector3andSpace moveUnitsPerSecond;
    9.         public Vector3andSpace rotateDegreesPerSecond;
    10.         public bool ignoreTimescale;
    11.         private float m_LastRealTime;
    12.  
    13.  
    14.         private void Start()
    15.         {
    16.             m_LastRealTime = Time.realtimeSinceStartup;
    17.         }
    18.  
    19.  
    20.         // Update is called once per frame
    21.         private void Update()
    22.         {
    23.             float deltaTime = Time.deltaTime;
    24.             if (ignoreTimescale)
    25.             {
    26.                 deltaTime = (Time.realtimeSinceStartup - m_LastRealTime);
    27.                 m_LastRealTime = Time.realtimeSinceStartup;
    28.             }
    29.             transform.Translate(moveUnitsPerSecond.value*deltaTime, moveUnitsPerSecond.space);
    30.             transform.Rotate(rotateDegreesPerSecond.value*deltaTime, moveUnitsPerSecond.space);
    31.         }
    32.  
    33.  
    34.         [Serializable]
    35.         public class Vector3andSpace
    36.         {
    37.             public Vector3 value;
    38.             public Space space = Space.Self;
    39.         }
    40.     }
    41. }
    42.  
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    It really isn't clear what that image is trying to show at all.

    Some observations which might be the source of your issue:
    • You're doing work per-frame but unless you're manually simulating physics per-frame it'll run during the fixed-update which might happen once, twice or several times perf-frame depending on settings and what's going on.
    • You're moving a Collider that is implicitly static (non-moving). If you want to move a collider then I would recommend adding a Kinematic Rigidbody to it.
    • You're direcly modifying the Transform when using Physics. Use the Rigidbody API and let it control the Transform pose (this is one of its roles).
    • When you modify a Transform, that doesn't change the Rigidbody pose. That'll happen when the simulation runs and it synchronises transform changes back to the physics system (this should be avoided where possible)
     
    JoeKee likes this.
  3. JoeKee

    JoeKee

    Joined:
    Feb 17, 2017
    Posts:
    3
    Thank you for your fast reaponse. Now I see also the problem with the picture: The Mouse.Pointer is not visible. It was in the middle of the cube and the ray should be in the center. Sorry for that.

    I will try to understand your remarks and will try to "refactor" the example.

    Okay, the picture is not so good, because the MousePointer is not visible :-( I have made some others and found another curious behaviour --- but may be it depends on your remarks. Please imagine the MousePointer on the Marker-Cube.

    -> If I use a marker as a child object it works like it should. But it shows also a strage think: the ray detects also hit.Points outside the Cube.

    -> If I use the marker not as a child it shows the behaviour described above.

    However, let me check your remarks first. Thanks again!
     

    Attached Files:

  4. JoeKee

    JoeKee

    Joined:
    Feb 17, 2017
    Posts:
    3
    Okay, cool. Thats it. I added a RigidBody to the root object, set it to Kinematic and changed the script using the rigidbody.MovePosition() method. It works quit perfectly.
    Thank you!
     
    Last edited: May 22, 2020
    MelvMay likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Nice. Make sure you set it to be Kinematic.
     
    JoeKee likes this.