Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DMD2542-SpaceShooterPart2-AndrewSides/Space Shooter/Assets/script/EvasiveManeuver.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
48 lines (42 sloc)
1.38 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class EvasiveManeuver : MonoBehaviour | |
{ | |
public Vector2 startWait; | |
public Vector2 maneuverTime; | |
public Vector2 maneuverWait; | |
public float dodge; | |
public float smoothing; | |
public Boundary boundary; | |
private float currentSpeed; | |
private float targetManeuver; | |
private Rigidbody2D rb; | |
void Start() | |
{ | |
rb = GetComponent<Rigidbody2D>(); | |
currentSpeed = rb.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(rb.velocity.x, targetManeuver, Time.deltaTime * smoothing); | |
rb.velocity = new Vector2(newManeuver, currentSpeed); | |
rb.position = new Vector2 | |
( | |
Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax), | |
Mathf.Clamp(rb.position.y, boundary.yMin, boundary.yMax) | |
); | |
} | |
} |