Hey Guys, I am converting the Project Space Shooter to 2D, and I’m in trouble in the enemy’s movement.
using UnityEngine;
using System.Collections;
public class EvasiveManeuver : MonoBehaviour
{
public Boundary boundary;
//public float tilt;
public float dodge;
public float smoothing;
public Vector2 startWait;
public Vector2 maneuverTime;
public Vector2 maneuverWait;
private float currentSpeed;
private float targetManeuver;
void Start ()
{
currentSpeed = GetComponent<Rigidbody>().velocity.y;
StartCoroutine(Evade());
}
IEnumerator Evade ()
{
yield return new WaitForSeconds (Random.Range (startWait.x, startWait.y));
while (true)
{
targetManeuver = Random.Range (1, dodge) * -Mathf.Sign (transform.position.x);
yield return new WaitForSeconds (Random.Range (maneuverTime.x, maneuverTime.y));
targetManeuver = 0;
yield return new WaitForSeconds (Random.Range (maneuverWait.x, maneuverWait.y));
}
}
void FixedUpdate ()
{
float newManeuver = Mathf.MoveTowards (GetComponent<Rigidbody>().velocity.y, targetManeuver, smoothing * Time.deltaTime);
GetComponent<Rigidbody2D>().velocity = new Vector3 (newManeuver, 0.0f, currentSpeed);
GetComponent<Rigidbody2D>().position = new Vector3
(
Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, boundary.yMin, boundary.yMax)
);
//GetComponent<Rigidbody2D>().rotation = Quaternion.Euler (0, 0, GetComponent<Rigidbody2D>().velocity.x * -tilt);
}
}