- Home /
2D Diablo style movement
Hello everybody!
I'm using the following script grabbed from here: http://wiki.unity3d.com/index.php/Click_To_Move_C This basically gives a very nice click to move control to my game, but I've got a problem with it. I'm making a 2D game for mobile, and for some reason it doesn't work if I try to use it in a "2D environment" (I tried the script with perspective camera in 3d and it worked!).
I've been struggling for hours and since I'm beginner I don't know how to modify this script to make it work. I basically have an ortographic camera, and a sprite on my scene which I attached the script onto. What else do I miss?
Thank you in advance!!!
It might be because of the vector3s in the script but i'm not 100% sure about that one. It might be those because a 2d game doesn't have any depth only x and y (or z).
2d uses the x and y axis. In a top-down game, x would be west-east and y would be north.south. The z axis is only used for things like Raycasting. Why is that important? Because the click to move script is indeed set up for a 3d environment, which you already know... And now you also will see how this is a problem. In a 3d environment, x is west-east as in 2d, but y is up-down and z is north-south. What you have to do is change the values assigned to the Vector3 variables like transform.position so they reflect the 2d environment.
It would also help if you posted what happens when you use the script in your 2d scene, ins$$anonymous$$d of just saying that it doesn't work :)
Answer by g2thet · Aug 09, 2017 at 06:42 AM
Try using this script as your player controller. This works in my 2d game. I found it in the following tutorial Click To Move in Unity 5. The example he creates in the tut is for 3d but it worked for my 2d game. Good luck and hope this helps!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
public class ClickToMove : MonoBehaviour
{
[SerializeField] [Range(1,20)]
private float moveSpeed = 5;
private Vector3 targetPosition;
private bool isMoving;
// Use this for initialization
void Start ()
{
targetPosition = transform.position;
isMoving = false;
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButton(0))
{
SetTargetPosition ();
}
if(isMoving)
{
MovePlayer ();
}
}
void SetTargetPosition()
{
targetPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
targetPosition.z = transform.position.z;
isMoving = true;
}
void MovePlayer()
{
transform.rotation = Quaternion.LookRotation (Vector3.forward, targetPosition - transform.position);
transform.position = Vector3.MoveTowards (transform.position, targetPosition, moveSpeed * Time.deltaTime);
if(transform.position == targetPosition)
{
isMoving = false;
}
}
}
Your answer
Follow this Question
Related Questions
[2D] Camera movement causes flickering/jittering sprites 2 Answers
Many cameras with Camera.main.ScreenToWorldPoint trouble. 2 Answers
Child of camera getting locked on position 0 Answers
Main Camera child under player, causes flip 3 Answers
My game is laggy, but there are only a ball and some flat platforms. 3 Answers