moving an object continuously between waypoints
hello, so I have a rotating cube shooting out bullets in a 2D bullet hell game. but I also wanted to set a movement pattern to it, so I set up waypoints for it to bounce around continuously until it's defeated. For some reason however, when I set the waypoints (which I have set as sphere objects) not only does it not move towards them, it stops rotating as well and I have no idea what I'm missing this is my code right now using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
public Transform [] Waypoints;
public float Speed;
public int curWaypoint;
public bool Patrol = true;
public Vector3 Target;
public Vector3 MoveDirection;
public Vector3 Velocity;
void Update ()
{
if(curWaypoint < Waypoints.Length)
{
Target = Waypoints [curWaypoint].position;
MoveDirection = Target - transform.position;
Velocity = GetComponent<Rigidbody>().velocity;
if(MoveDirection.magnitude < 1)
{
curWaypoint++;
}
else
{
Velocity = MoveDirection.normalized * Speed;
}
}
else
{
if(Patrol)
{
curWaypoint = 0;
}
else
{
Velocity = Vector3.zero;
}
GetComponent<Rigidbody> ().velocity = Velocity;
{
transform.Rotate (new Vector3 (0, 0, 300) * Time.deltaTime);
}
}
}
}
If you see something that I don't I'd appreciate you telling me how to go about it! Thank you!
Your answer
Follow this Question
Related Questions
Some problems with handmade basic player controller 0 Answers
How to access y rotation of an object as a variable or value 1 Answer
Smooth Rotation on WASD keys pressed? 1 Answer
How do you change your z axis in a movement script. 1 Answer
Issue with wave-like motion when rotating with mouse input 0 Answers