Character movement with click
I'm a completely beginner at game developing.
I've browse through tutorials on internet about 2d game with unity, but mostly its about platformer or something similar (movements are right-left-jump, means not the one i'm looking for).
What i wanna make is 'diner dash' like game, when player click on a table(or anything) the player is move toward the object, and do something (in diner dash case, she take customer's order). and most of the tutorial that i've watched the controller is using keyboard. So, any suggestion where i have to start? Any keyword for google search?
sorry for my engrish :(
Answer by Salmjak · Feb 18, 2016 at 12:06 PM
To translate mouse position to world-coordinates you would want to use a Raycast. So that's how you will know where the player clicks. The code would look like this (you can also check out this video):
float speed = 10f;
float rotationSpeed = 20f;
void Update () {
if (Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)
{
Vector3 newPos = Vector3.MoveTowards(transform.position, hit.point, Time.deltaTime * speed);
Vector3 _direction = (hit.point - transform.position).normalized;
Quaternion lookDirection = Quaternion.LookRotation(_direction);
lookDirection.x = 0;
lookDirection.z = 0;
gameObject.transform.rotation = Quaternion.Slerp(transform.rotation, lookDirection, Time.deltaTime * rotationSpeed);
transform.position = newPos;
}
}
}
In the above code we first check if the player is pressing the left mouse button. Then we create a ray using the mouse position (ScreenPointToRay translated the screen point to a world point and direction). If use the ray in a Raycast, which will return a RaycastHit (hit) if it hits something. We then create a new position, that will be somewhere between transform.position and the place we hit with a max distance of Time.deltaTime * speed (you will have to set the speed as a float yourself). Then we use some quaternions and normalized vectors to get a direction to look at. This is not needed for the movement, but it looks nice when you character turns.
And finally we set our transform.position to the new position, and since this is in the Update()-function it will run every frame and you will move closer and closer to the goal.
And for a move on click function you would loop this code after the player releases the button. It would look like this:
void Update(){
if (Input.GetMouseButtonUp(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
StartCoroutine(MoveToPoint(hit.point));
}
}
}
float speed = 10f;
float rotationSpeed = 20f;
IEnumerator MoveToPoint(Vector3 newPos)
{
Vector3 _direction = (newPos - transform.position).normalized;
Quaternion lookDirection = Quaternion.LookRotation(_direction);
lookDirection.x = 0;
lookDirection.z = 0;
while (Vector3.SqrMagnitude(transform.position - newPos) > 0.5f)
{
Vector3 nextPos = Vector3.MoveTowards(transform.position, newPos, Time.deltaTime * speed);
gameObject.transform.rotation = Quaternion.Slerp(transform.rotation, lookDirection, Time.deltaTime * rotationSpeed);
transform.position = nextPos;
yield return new WaitFixedUpdate();
}
}
Where you again will have to set the float speed and float rotationSpeed youself.
Define "2D", your description sound like a top-down game, so yes. It will not work on a platformer (which you said you already have tutorials for).
It CAN work in 2D-platformer style if you rework it somewhat, replace Vector3 with Vector2 and find a good way to the find the correct position to move to (so you don't magically move up in the air), you can still use Raycast as I did but make sure to add the suffix "2D" to all physic-function (ex. Rigidbody2D, Raycast2D, RaycastHit2D, etc). But the basis of looping the movement is still the same :)
Your answer
Follow this Question
Related Questions
standard Assest 1 Answer
How to substitute one object with another in animation clip 0 Answers
Why is my Unity different than the one in the Beginner Tutorial? 1 Answer
Accessing Channel Values in Color Correction Curves 0 Answers
I have starded the basic tutorial and got this bug. Cannot move further because of this. 0 Answers