- Home /
how to move player with WASD keys?
i want to move my player (blue player) through those waypoints (blue gameobject) and reach the end point (orange gameobject) by using WASD keys. i tried using MoveTowards but was able to move player in only one direction and i dont know how to move the player through all those waypoints. Here's my code
public float speed = 1f; public Transform GameObject;
private void Update()
{
float step = speed * Time.deltaTime;
if (Input.GetKey(KeyCode.D))
{
transform.position = Vector2.MoveTowards(transform.position, GameObject.position, step);
}
}
Answer by LCStark · Dec 18, 2018 at 08:41 AM
transform.position = Vector2.MoveTowards(transform.position, GameObject.position, step);
This code will only move your object in the direction of the attached GameObject
transform. You will have to either find a better way to specify your target, or you will have to define more attached transforms and switch them as the targets in your movement functions.
Vector2.MoveTowards
takes three parameters: current position, target position and maximum movement step (in this single frame). It returns the new position after moving towards the target. If you compare that returned position to your target, you will be able to tell when your object has reached the target. At that point you can choose to stop moving for now and change the target coordinates to the next waypoint position.
Check out the MoveTowards
documentation here, it has a nice example of setting the target to the position of a mouse click, it should give you some ideas on how to solve this.
Oh, and you might want to change the name of your transform. GameObject
is already used by Unity, if you'll want to use its functionality in the future, make sure your transform doesn't override that name.
Ty for your answer. Since i'm new to unity and coding i would like to see an example for what you have said so i can understand it better. Ty
Have you checked this link? It has a pretty decent and simple example of modifiable target.
Try something like this:
private Vector2 currentTarget;
private void Update() {
float step = speed * Time.deltaTime;
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
transform.position = Vector2.$$anonymous$$oveTowards(transform.position, currentTarget, step);
if (transform.position == currentTarget) {
// Set a new `currentTarget`
}
}
}
You could set your
currentTarget
to some initial value in the
Start
method, or have it public and set the starting value from the Inspector, or get its value from another object (like your waypoint transforms).
You could add all of your waypoints to a list for easy access. Ins$$anonymous$$d of a single
public Transform GameObject
put all of them in a
public List<Transform> waypoints
. That way you'll be able to get them all in your Update method (`waypoints[0].position, waypoints[1].position, ...).
Ty for your answer but i'm still unable to figure out how to move player in 4 directions. Right now the player moves only to the left when pressing 'A" key . Here's my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Waypoints : $$anonymous$$onoBehaviour { public float speed = 10f; private Transform target; private int wavepointIndex = 0;
private void Start()
{
target = Red.points[0];
}
private void Update()
{
float step = speed * Time.deltaTime;
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
transform.position = Vector2.$$anonymous$$oveTowards(transform.position, target.position, step);
if (Vector2.Distance(transform.position, target.position) <= 0.010f)
{
wavepointIndex++;
target = Red.points[wavepointIndex];
}
}
}
}
And the player goes a little ahead of the waypoint whenever i press the key.
Answer by thleosw · Dec 18, 2018 at 08:11 AM
Hi, you could check out the function Input.GetAxis: https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
It reads the input from the arrows or wasd keys
Your answer
Follow this Question
Related Questions
Player movement 1 Answer
Player movement script problem 1 Answer
How to make Jump 2D Action Stop after finite amount of Time? 1 Answer
Unintended bounce on 2D platformer 1 Answer