Raycasts are not working on Timescale=0

Hi there, I am trying to make an examine system and during the examine, I want to set the timescale to zero.
I use raycast for object visibility test.(Frustum culling aabb test is not enough, since user must see the object too)

Normally, if timescale is 1, it all works fine and dandy, but if I set it to 0 or very low like 0.01, it doesn’t detect the collider that ray suppose to hit. I think this is some sort of bug in Unity.

Is there a setting or something that I can set to fix this/workaround it?

Also alternative visibility suggestions are welcome too.

Are you doing raycasts in FixedUpdate ?
Show us your code.

Raycasts are not affected by timescale. Check if your code is running with log statements.

I also have the same issue, when the timescale is set to 0.001, the physics raycast is not detecting the player by the enemy. I have created a viewcone for the enemy. And the viewcone is not detecting the player, when the time scale is 0.001. Its working fine when timescale is 1. Can someone please help me why its not working when time scale is too low.

Here is my Script for the raycast:

using System.Collections;
using System.Collections.Generic;
using UnityEditor.Rendering;
using UnityEngine;

public class Test_CubeCollisionDetector : MonoBehaviour
{
    [SerializeField]
    GameObject player;
    [SerializeField]
    float TimeScaleValue;
    public MeshFilter viewconeMeshFilter;
    private Mesh viewconeMesh;
    public int numberOfRays = 10;
    public float angleRange = 30f;
    [SerializeField]
    float maxDistance;
    private List<Vector3> hitPositions = new List<Vector3>();
    private LayerMask groundlayer1mask;

    // Start is called before the first frame update
    void Start()
    {
        groundlayer1mask = LayerMask.GetMask("Ground");
    }
    private void Awake()
    {

        viewconeMesh = new Mesh();
        viewconeMeshFilter.mesh = viewconeMesh;
    }
    private void CreateMesh()
    {
        int[] triangles = new int[hitPositions.Count * 3];
        Vector3[] vertices = new Vector3[hitPositions.Count + 1];


        for (int i = 0; i < hitPositions.Count; i++)
        {
          
            vertices[i + 1] = viewconeMeshFilter.transform.InverseTransformPoint(hitPositions[i]);

        }

        // Set the triangles
        for (int i = 0; i < hitPositions.Count - 1; i++)
        {
            triangles[i * 3] = 0;
            triangles[i * 3 + 1] = i + 1;
            triangles[i * 3 + 2] = i + 2;
        }


        viewconeMesh.Clear();
        viewconeMesh.vertices = vertices;
        viewconeMesh.triangles = triangles;
        viewconeMesh.RecalculateNormals();
    }
    private void CastRays()
    {
        hitPositions.Clear();

        float angleStep = angleRange / (numberOfRays - 1);
        Quaternion rotation = transform.rotation * Quaternion.Euler(0f, -angleRange / 2f, 0f);

        for (int i = 0; i < numberOfRays; i++)
        {
            Vector3 direction = rotation * Vector3.forward;
            RaycastHit hit;
            Vector3 p;
            if (Physics.Raycast(transform.position, direction, out hit, maxDistance, groundlayer1mask))
            {
                hitPositions.Add(hit.point);
                p = hit.point;
                Debug.Log("player found");
            }
            else
            {
                p = transform.position + direction * maxDistance;
                hitPositions.Add(transform.position + direction * maxDistance);
            }
            rotation *= Quaternion.Euler(0f, angleStep, 0f);
        }
    }
    // Update is called once per frame
    void Update()
    {
        Time.timeScale = TimeScaleValue;
        CastRays();
        CreateMesh();
    }
}

Please don’t necro posts, simply create your own. As the above already said, the time-scale has nothing to do with physics queries and it doesn’t stop them working.

What doesn’t happen when the time-scale is zero is that the physics simulation (and other stuff) isn’t running because no FixedUpdate are being called as you should expect.

This means if you’re changing the Transform to “move” physics things (which you shouldn’t do in the first place) then those Transform changes won’t update physics.

NOTE: Your CreateMesh code is going to create an awful lot of garbage as you’re constantly creating arrays then just leaving them to the GC. This isn’t going to scale well.

the problem is this. if you use transform.position to move your object, while timescale=0, the physics engine does not register the new position of the colliders on the object. thats why your raycast hits nothing.

the solution is to use call Physics.SyncTransforms(). this tells the physics engine to update all collider positions. it is equivalent to what happens during FixedUpdate.

(PS this is not necroposting. a thread should remain active until a proper answer has been found. there is no point in starting new threads with the same question, and no answers. all threads show up on googlesearch and multiple threads on the same topic just make it more difficult to find an answer.)

It’s still necro-posting but I’m not moderating the forums about that anymore, it’s an endless flood that can’t be stopped so have at it. :wink: