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.
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: