Commit 4e779c8a by Philipp Adolf

Change rotation of rockets

Interpolation is now done between the current and target rotation, so that the magnitude of the velocity is constant.
parent 83bf8ea8
...@@ -6,8 +6,8 @@ public class Rocket : MonoBehaviour ...@@ -6,8 +6,8 @@ public class Rocket : MonoBehaviour
#region members #region members
public int damagePoints = 15; public int damagePoints = 15;
public float flyToTargetSpeed = 10f; public float rotationSpeed = 5f;
public float speed = 1f; public float speed = 5f;
public float pushForce = 20f; //wie stark das target weggeschubst wird beim aufprall. public float pushForce = 20f; //wie stark das target weggeschubst wird beim aufprall.
private Rigidbody2D body; private Rigidbody2D body;
...@@ -26,10 +26,11 @@ public class Rocket : MonoBehaviour ...@@ -26,10 +26,11 @@ public class Rocket : MonoBehaviour
if (!target) return; if (!target) return;
//update direction: //update direction:
Vector3 direction = target.position - transform.position; Quaternion targetRotation = Quaternion.FromToRotation(Vector2.up, target.position - transform.position);
body.velocity = Vector2.Lerp(body.velocity, (float)speed * direction.normalized, Time.deltaTime * flyToTargetSpeed); Quaternion newRotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
Quaternion newRotation = Quaternion.Euler(0, 0, -Mathf.Rad2Deg * Mathf.Atan2(direction.x, direction.y));
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * flyToTargetSpeed); body.velocity = newRotation * (speed * (Vector2.up));
transform.rotation = newRotation;
} }
void OnTriggerEnter2D(Collider2D other) void OnTriggerEnter2D(Collider2D other)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment