- Home /
Question by
chinanacaixa · Oct 08, 2013 at 08:50 AM ·
movementmouseclickrpg-game
click to move error
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);
}
}
Comment
The information you gave us isn't helpful enough. Please see the line number where this error occurs, and put a comment on where that is in the code you posted. Also, see this wonderful explanation about the NullReferenceException
exception from Jamora.
Answer by meat5000 · Oct 08, 2013 at 09:06 AM
Mouse Position is a Vector2 and you need a Vector3. Just put ', 0' after your Input.mousePosition
(Input.mousePosition, 0)
Your answer
![](https://koobas.hobune.stream/wayback/20220613115523im_/https://answers.unity.com/themes/thub/images/avi.jpg)