"Click to move" lecture discussion

What error are you receiving? @dd17happy and @Hansel_de_Leon.

@Kamil, it will only work if you have the brackets selected when you press it. Try click in between the two ().

1 Like

Sir I have error NullReferenceException: Object reference not set to an instance of an object
Mover.MoveToCursor () (at Assets/script/Mover.cs:20)
Mover.Update () (at Assets/script/Mover.cs:14)
When I click

You have to make a script for the Main Camera where it follows the player and also has controls to rotate around aswell as pitch up and down, thats where you add WASD and Arrow keys functionality aswell as scroll wheel zooming.

Im having the following problem, for some reason, Unity is telling me that it doesn’t recognize RayCastHit. I have no idea of what’s going on, and i have not found a solution on my own to this problem, i will keep trying but i will share Screenshot of the problem and my code.

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;

    }

}

}

Sorry about the bad look of the code, right now i don’t remember how to show it pretty when i copy and paste.

P.S: I forgot to mention im using Unity 2021.3.3f1

I said nothing, i mistype RaycastHit… Anyway, im not one to retract when im not in my brightest moments, so if someone could tell me at less how to make my code pretty i would be thankful.

Privacy & Terms