- Home /
Player movement script problem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public float speed = 1f;
private Transform target;
private int wavepointIndex = 0;
private void Start()
{
target = Red.points[0];
}
private void FixedUpdate()
{
float step = speed * Time.deltaTime;
if (Input.GetKey(KeyCode.W))
{
transform.position = Vector2.MoveTowards(target.position,new Vector2(0 ,transform.position.y), step);
wavepointIndex++;
target = Red.points[wavepointIndex];
}
if (Input.GetKey(KeyCode.S))
{
transform.position = Vector2.MoveTowards(target.position , new Vector2(0, -transform.position.y ), step);
wavepointIndex--;
target = Red.points[wavepointIndex];
}
if (Input.GetKey(KeyCode.D))
{
transform.position = Vector2.MoveTowards(target.position ,new Vector2(transform.position.x , 0), step);
wavepointIndex++;
target = Red.points[wavepointIndex];
}
if (Input.GetKey(KeyCode.A))
{
transform.position = Vector2.MoveTowards(target.position , new Vector2(-transform.position.x, 0), step);
wavepointIndex--;
target = Red.points[wavepointIndex];
}
}
}
////////////Red script using System.Collections.Generic; using UnityEngine;
public class Red : MonoBehaviour {
public static Transform[] points;
void Awake()
{
points = new Transform[transform.childCount];
for (int i = 0 ; i < points.Length ; i++)
{
points[i] = transform.GetChild(i);
}
}
}
My problem is i'm trying to move player (blue circle) through those waypoints (red game objects) using WASD movement but the pleayer is moving too fast . For instance if i press A it goes to the last waypoint instead of going to the leftmost game object i have tried using raytracing but found no solution would like some insights on what im doing wrong.TY
I made this project for you https://www.dropbox.com/s/03cmtrel9oxz9un/$$anonymous$$iteshk7.zip?dl=1 Download it and tell me if it helps. $$anonymous$$ake sure to open up the scene titled 'Default'.
this works only for 4 targets and if i want to make it work for multiple gameobjects then mapping WASD keys for every gameobject wont be possible , if i press W the player should go in 1 direction but in this case and even in my script player moves downwards , sidewards when pressed W
Here, I modified the project. Tell me what you think https://www.dropbox.com/s/tbvpvtwo5x5j104/$$anonymous$$iteshk7.zip?dl=1
Answer by Snipe76 · Jan 28, 2019 at 04:26 PM
I see what you are trying to do.
But I think the problem is that you use Input.GetKey instead of Input.GetKeyDown in the FixedUpdate method.
I'm using Input.Get$$anonymous$$ey only. and i tried using Input.Get$$anonymous$$eyDown and the player is not moving when pressed D and hardly moves when pressed W
Its because the method $$anonymous$$oveTowards needs to be called with each update.
and by putting it behind Get$$anonymous$$eyDown will make it be called only once.
You need to change the way the player moves towards the waypoints to achieve your goal.
would be more helpful if you can show how to do it in code
put your movement method in Update() ins$$anonymous$$d of FixedUpdate()
i would also try vector3 ins$$anonymous$$d of vector 2, i know you're in a 2D game but my movement methods always work better in vector3 code ins$$anonymous$$d of vector2
as per your suggestion i used Vector3 but the player still moves in all directions when pressed any key and player gets stuck in the last gameobject and when S key is pressed at that point the player doesnt go backwards.
I'll test out your script as is tomorrow. going to be fir the night.
Your answer
Follow this Question
Related Questions
Player movement 1 Answer
how to move player with WASD keys? 2 Answers
How do I do a Snappy Jump in 2D with my 2D Jump Script? 1 Answer
Player Dash doesn't work properly 0 Answers