Skip to content
Permalink
ae9d50a352
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
48 lines (42 sloc) 1.38 KB
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)
);
}
}