0
votes

I'm trying to make a very simple race game with spheres, however, I face many problems.

First of all, I'm trying to make a very simple AI system for opponents.

The problem I have here is that I want opponents to detect obstacles and avoid them using Raycast but only a certain type of obstacle , a simple cube is detected.

I've created a simple sphere as opponent and wrote a script so it can move and detect obstacles Here is update function:

void FixedUpdate()
{
    transform.Translate(Vector3.forward * Time.deltaTime * speed);

    if (Physics.Raycast(transform.position, transform.forward, 100.0f))
        print("There is something in front of the object!");

}

The message is printed only when there is a cube forward and it does not detect any other obstacles. What can be so wrong? Also, is there any idea how to move left or right when opponent raycast an obstacle?

obstacle that is detected

hierarchy

cube01 that is child of obstacle2 that is not detected

3
No idea, can't help you if you don't tell us about your hierchy and specifically the different between the objects it does detect and the objects it doesnt detect. - AresCaelum
"Also, is there any idea how to move left or right when opponent raycast an obstacle?" You also have to do raycast to transform.right and -transform.right. In fact, you should be using NavMesh for this. Start with this tutorial. - Programmer
@Eddge I've added some screenshots. Are they enough? - Maria Kodel
@Programmer Thanks !!! - Maria Kodel
Try making their x and y the same? Based off of your code you are not turning (or strafing), so it is possible your ray cast is missing, you can also use Debug.DrawLine in your normal update to see where the raycast is. docs.unity3d.com/ScriptReference/Debug.DrawLine.html - AresCaelum

3 Answers

1
votes

Only collider components are detected using raycast, make sure you have an appropriate collider (size of the collider does not necesarily match size of the mesh that gets rendered). Normally also layers on which objects are are important but syntax you are using is not checking for layer mask anyway

0
votes

You can use Debug.DrawLine() or Debug.DrawLine() to debug the line and see if it cross the obstacles or not. Here is the documentation for them DrawRay DrawLine For moving right and left I think you can add something like this

void FixedUpdate()
{
    Vector3 dir = Vector3.forward * Time.deltaTime * speed;
    if (Physics.Raycast(transform.position, transform.forward, 100.0f))
    {
        print("There is something in front of the object!");
        dir += Vector3.Right * horizontalSpeed * Time.deltaTime;
    }
}

You might also consider ray casting two rays to detect the direction to lean to if it will be the left or the right.

0
votes

Unity Physics Best Practices (As in https://unity3d.com/pt/learn/tutorials/topics/physics/physics-best-practices) recommends that we don't use Raycasts in FixedUpdate, as it is heavy to process and may not always work.

There is also some tips about Matrix Layers that will help you improve performance and avoid such bugs.

Good luck!