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 *= 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