Issues With Ray cast hit

I’m having difficulty in getting ray cast hit to trigger anywhere other than right on the Player object.

I can see the nave mesh agent covers the terrain.

When I click anywhere while the game is running nothing happens. If I click very close to the Player object - My player object will move that distance and then stop. If i move the player object far away from where i clicked it will move to that spot

Hmm, let’s do some digging and see what’s wrong. What does the code in your InteractWithMovement() method look like?

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

public class Mover : MonoBehaviour

{

[SerializeField] Transform target;

void Update()

{

    if (Input.GetMouseButtonDown(0))

    {

        MoveToCursor();

    }

}

private void MoveToCursor()

{

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    RaycastHit hit;

    bool hasHit = Physics.Raycast(ray, out hit);

    if (hasHit)

    {

        GetComponent<NavMeshAgent>().destination = hit.point;

    }

}

}emphasized text

I suspect it might be because im using Physics. with a plane object. When i think Physics. should be for a Terrain object

The script looks right, so it may be an issue with the scene setup. Let’s slip in a Debug.Log to see if we can get an idea what might be going on…

In MoveToCursor, modify the if(hasHit) as follows:

if(hasHit)
{
     Debug.Log($"Hit.point = {hit.point}");
} else
{
     Debug.Log($"No hits detected."
}

nothing hits accept for the Player object and the Target object anywher els on the plane it returns no hits.

If i make the target object span the length of the plane it will move along the mesh which makes me think there is no 3d object attached to the plane. I made a 3d plane and imported it from blender?

If It’s just a plane, why did you make one in Blender?

Does your plane have a Mesh Collider?

Thank you Brian that was it the Mesh collider. I was playing around in Blender and low poly terrain wanted to use that instead of making a new one in unity

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms