Hello, I’m trying to implement the mouse-click movement on my rpg, i studied this script and it seems good aparently, but when i will run the game appears that error
“NullReferenceException
UnityEngine.Camera.ScreenPointToRay (Vector3 position)”
the problem is in the code or in the object and your coordenates, or whatever ?
public float PlayerMoveSpeed;
private Vector3 TargetPosition;
private Transform PlayerTransform;
void Start ()
{
PlayerTransform = transform;
TargetPosition = PlayerTransform.position;
}
void Update ()
{
//Amount to move, move speed
float amtToMove = PlayerMoveSpeed * Time.deltaTime;
//Move the Player after they have clicked the Left Mouse Button on a location
if (Input.GetMouseButtonDown(0))
{
Plane playerPlane = new Plane(Vector3.up, PlayerTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
TargetPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
PlayerTransform.rotation = targetRotation;
}
}
PlayerTransform.position = Vector3.Lerp(PlayerTransform.position, TargetPosition, amtToMove);
}
}