Using Raycast hit to set vector position then moving player to that position

Hi All,

I know there are many topics on this area however I am yet to find a solution. I’m new to unity, am grasp the logic behind the code OK however am struggling with trying to use Raycast Hit for a none flat terrain.

my scene link

[Tutorial I’m Following] (Unity3D: RPG Tutorial (Diablo Style) Session 1(Updated Version): Click To Move - YouTube)

I’ve made some small changes to the namimg conventions but apart from that everything is exact from what I can tell.

The SimpleMove function doesn’t appear to work. No errors are received when running the script. All the player does is rotate that it. Doen’t move from it’s position.
Have tried using other methods (one is commented out in the below code) but to no avail. Would really like to learn more as I progress through this tutorials. Help very much appreciated.

This method works but I want to use the Raycast hit to detect if it’s an enemy or an item I have clicked on.

Have tried playing around with navmesh agents instead where character can move around but can’t get the navmesh and raycast hit working. Any help again appreciated.

The code I have below.

void Awake()
{
    //anim = GetComponent<Animator>();
}

void Start()
{
    targetPosition = transform.position; // will set the target position to our current position on start
}

// Update is called once per frame

void Update()
{
    //if the player clicked on the screen, find out where
    if (Input.GetMouseButton(LEFT_MOUSE_BUTTON))
    {
        SetTargetPosition();
    }
    MovePlayer();
}

void SetTargetPosition() // set position we will travel too
{
    RaycastHit hit;
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, out hit, 1000))
    {
        targetPosition = new Vector3(hit.point.x, hit.point.y, hit.point.z);

    }
}

void MovePlayer()
{
    if (Vector3.Distance(transform.position, targetPosition) > 1)
    {
        Quaternion newRotation = Quaternion.LookRotation(targetPosition - transform.position, Vector3.forward);

        newRotation.x = 0f;
        newRotation.z = 0f;

        transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
        controller.SimpleMove(transform.forward * walkspeed);
        //transform.position = Vector3.MoveTowards(transform.position, targetPosition, walkspeed * Time.deltaTime);
        Debug.DrawLine(transform.position, targetPosition, Color.red);
    }
}

}

Hi

I’m not sure if it could be causing the issue, but to start with your quaternion code is definitely bad! :slight_smile:

You can not simply set the x/z values of a quaternion. Perhaps you’re confusing it with the euler angles (displayed in the inspector)? I suspect what you’re trying to do is point the rotation at the target in terms of y rotation, but leave x and z rotation alone? For this, the simplest approach would be to just make your ‘target’ be at the same z as your transform:

           void MovePlayer()
            {
                Vector3 currentTarget = targetPosition;
                currentTarget.y = transform.position;

                if (Vector3.Distance(transform.position, currentTarget) > 1)
                {
                    Quaternion newRotation = Quaternion.LookRotation(currentTarget - transform.position, Vector3.forward);

                    transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);

                    controller.SimpleMove(transform.forward * walkspeed);
                    //transform.position = Vector3.MoveTowards(transform.position, targetPosition, walkspeed * Time.deltaTime);

                    Debug.DrawLine(transform.position, currentTarget, Color.red);
                }
            }

I’m not sure exactly what the controller.SimpleMove does - presumably you have another script that the controller is referencing, so that could be screwing with your transform. Without knowing more about it I couldn’t say though.

All that said, just to be sure, have you made 100% certain that walkspeed is not 0? (just checking!)

-Chris

Answer in my comment. Have used navMeshAgent instead for pathfinding wilst still be able to use a Raycast Hit to determine what object i’ve hit with the raycast