- Home /
Question by
eminem1984 · Jul 24, 2017 at 03:42 AM ·
space shooterdodge
the enemy ship keep bumping into the asteroids
Hey guys,
now i have come across another problem - even I wrote the correct codes about the dodge thing in the maneuver script according to the tutorial and also set up the same speed as the asteroids are, however the enemy ship still keep crashing with some of the asteroids when it tilts. here i've attached the script, and the inspector information, any ideas?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyManeuver : MonoBehaviour {
private float newManeuver;
private float targetManeuver;
public Vector2 Start_wait;
public Vector2 maneuverTime;
public Vector2 maneuverWait;
public float swing;
public float speed;
private Rigidbody rb;
public Boundary b;
public float tilt;
private Vector3 currentSpeed;
void Start () {
rb = GetComponent<Rigidbody> ();
currentSpeed = rb.velocity;
StartCoroutine (Evasion ());
}
IEnumerator Evasion () {
yield return new WaitForSeconds (Random.Range(Start_wait.x, Start_wait.y));
while (true) {
targetManeuver = Random.Range (1, swing) * - 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 () {
newManeuver = Mathf.MoveTowards (rb.velocity.x, targetManeuver, Time.deltaTime * speed);
rb.velocity = new Vector3(newManeuver, 0.0f, currentSpeed.z);
rb.position = new Vector3(Mathf.Clamp(rb.position.x, b.xMin, b.xMax), 0.0f, Mathf.Clamp(rb.position.z, b.zMin, b.zMax));
rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * - tilt);
}
}![alt text][1]
[1]: /storage/temp/98224-capture.png
capture.png
(83.7 kB)
Comment