1
\$\begingroup\$

I've made a totally new scene for a 2D mobile game. I placed a sprite on the center of the screen and attached the following script onto it (this is basically a diablo-style click-to-move control script, the sprite follows the pointer when it is clicked):

using UnityEngine;

public class Movement : MonoBehaviour
{
private float _moveSpeed = 5;
private Vector3 _targetPosition;
private float _targetDistance;

void Update()
{
    _targetDistance = Vector3.Distance(_targetPosition, transform.position);

    if (_targetDistance < 1)
    {
        _moveSpeed = 0;
    }
    else if (_targetDistance > 1)
    {
        _moveSpeed = 5;
    }

    if (Input.GetMouseButton(0))
    {
        Plane playerPlane = new Plane(Vector3.up, transform.position);
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        float hitdist = 0.0f;

        if (playerPlane.Raycast(ray, out hitdist))
        {
            _targetPosition = ray.GetPoint(hitdist);
        }
    }

    if (_targetDistance > 1)
    {
        transform.position += (_targetPosition - transform.position).normalized * _moveSpeed * Time.deltaTime;
    }
}
}

Normally this script doesn't work unless I rotate the main camera by 90 degrees (axis: X) and rotate the sprite also by 90 degrees (axis: X).

How should I modify the script to make it work without rotating the camera and all the sprites I place on the scene?

Thank you in advance!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

When working spatially, especially with hidden geometry (such as the Plane you generate for receiving raycasts from the screen's mouse pos to the scene), you want to be absolutely sure you're getting what you expected.

Here's what your plane looks like:

Vector3.up looks like this, from the camera with default rotation

Here's how it should be:

Vector3.forward looks like this, from the same scene

In the line Plane playerPlane = new Plane(Vector3.up, transform.position);

Change Vector3.up to Vector3.forward, as your camera is (assumed) facing foward you need to set the orientation of the plane to the same in order to use it as a flat surface!

Remember that you can visually debug geometric problems with Unity's wonderful Gizmos and Handles classes!

\$\endgroup\$
5
  • \$\begingroup\$ Please explain (in your answer) why you chose Vector3.Forward. \$\endgroup\$
    – Jon
    Commented Mar 15, 2016 at 0:19
  • \$\begingroup\$ Because it works.. Simple boy @Jon :D \$\endgroup\$ Commented Mar 15, 2016 at 0:23
  • \$\begingroup\$ @HamzaHasan, this isn't a common-core ScanTron test... The OP needs to understand the mistake to truly progress. \$\endgroup\$
    – Jon
    Commented Mar 15, 2016 at 0:31
  • 1
    \$\begingroup\$ I know @Jon. The theory is normal or direction vector doesn't change. So for Vector3.up which is actually (0,1,0) represents the Y coordinates not gonna change, which leads move sprite in X and Z axis. Similarly Vectro3.forward is (0,0,1), so in that case Z is not going to change but only X and Y, that is the desired behaviour. BTW answer is not mine, but my answer is same with bit more explanation \$\endgroup\$ Commented Mar 15, 2016 at 0:34
  • \$\begingroup\$ @HamzaHasan, if you're trying to say "the plane's normal and the terrain's normal need to match", you still haven't; just more about vectors. +1 for answer-in-comment? Since when? \$\endgroup\$
    – Jon
    Commented Mar 15, 2016 at 1:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .