- Home /
Evasive Maneuver to Space Shooter 2D.
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);
}
}
I need an AI to move the Enemy, as well as the Space Shooter, but in 2D editor, tried to use this script but does not work 100%.
This is what should happen:https://www.youtube.com/watch?v=kX0hnOS1QQQ&list=PLX2vGYjWbI0RibPF7vixmr4x8ONJX-mNd
This is what happens: https://www.youtube.com/watch?v=RtgemNs$$anonymous$$7d0&feature=youtu.be
Sorry Jonatas, please don't get discouraged. I suggest you do delete this one, then improve the question with more details and post it again.
Answer by napilnat · Jan 23, 2016 at 08:47 PM
The problem with this one is that the enemy does not move in y axis. it does not maneuver. is this a side scrolling space shooter?
Your answer
Follow this Question
Related Questions
Fire limit in a Space shooter? 1 Answer
How to remove a specific prefab from a List? 1 Answer
2d bullet shot at bullet emitter 0 Answers
Lasers wont stop firing?? 1 Answer
Multiple Cars not working 1 Answer