Need help... player always moves to center of screen instead of where I click

Hello,

I’m experiencing an issue with my Unity project where my player (cube for testing) is always moving towards the center of the screen instead of where I click. It reaches the center and refuses to move again? I’ve tried creating a new blank project, but the issue persists.

Here are some details about my project and the issue I’m experiencing:

  • Unity version: 2020.1.6f1
  • Platform: Windows
  • Only one script (Move.cs) is attached to the cube
  • No error messages or warnings in the Unity console
  • No other input controls in the scene that could be interfering with the mouse input

Here is the only script in my project:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour
{
    public float speed = 5f;
    private Vector3 target;

    // Start is called before the first frame update
    void Start()
    {
        target = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            target.z = transform.position.z;
        }

        transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    }

}

If anyone has experienced a similar issue or has any suggestions for how to resolve this, I would greatly appreciate your help.

Thank you!

PS: I have never used these forums before. So I am very sorry if anything is wrong or out of place.

Go check the docs for this method:

You are passing mousePosition, which will be (x,y,0)

You need to give it an (x,y,z) where z is how far into the the scene you want the world point to be.

Copy mousePosition out to a temp Vector3, change the .z to what you want, then pass that temp Vector3 into ScreenToWorldPoint.

Docs lay it all out. :slight_smile:

Thank you so much! it works perfectly!! :smile:

I followed a tutorial on YT and it worked flawlessly for him… somehow? : /

Thanks again for the rapid reply.

Could be he had his camera both set to Orthographic and set at Z == 0, and then had his near / far clip plane set to perhaps -1 and +1 ?

I’m watching it again, although he doesn’t mention it, it was set to Orthographic!

Thanks again btw :):slight_smile:

1 Like

Here’s why: the Editor can be in 2D mode or not (see Default Behaviour mode in Edit → ProjectSettings → Editor)… when in 2D, Cameras created are in Orthographic. When in 3D, they are perspective. Try it right now, it’s super-easy to flip it back!

BAM! If you dig far enough, usually you can get to the bottom of stuff. It will take time, but it does feel good when you get to the bottom of it.

ALSO: reading and re-reading code is essential… until this moment I had failed to notice line 22 above, which takes care of the actual Z-positioning, but does not fix the camera’s projection matrix.

Keep at it and welcome! Go nuts, make lots of games, Unity will both frustrate and reward you immensely. I’m probably the biggest Unity tankie on these forums and you sound like the type of person who will get into it.

Oh yeah, check out this six-minute video… I love his approach:

Imphenzia: How Did I Learn To Make Games:

Thanks :smile: