MechSpiderController.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.Demos {
  4. /// <summary>
  5. /// Controller for the Mech spider.
  6. /// </summary>
  7. public class MechSpiderController: MonoBehaviour {
  8. public MechSpider mechSpider; // The mech spider
  9. public Transform cameraTransform; // The camera
  10. public float speed = 6f; // Horizontal speed of the spider
  11. public float turnSpeed = 30f; // The speed of turning the spider to align with the camera
  12. public Vector3 inputVector {
  13. get {
  14. return new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  15. }
  16. }
  17. void Update() {
  18. // Read the input
  19. Vector3 cameraForward = cameraTransform.forward;
  20. Vector3 camNormal = transform.up;
  21. Vector3.OrthoNormalize(ref camNormal, ref cameraForward);
  22. // Moving the spider
  23. Quaternion cameraLookRotation = Quaternion.LookRotation(cameraForward, transform.up);
  24. transform.Translate(cameraLookRotation * inputVector.normalized * Time.deltaTime * speed * mechSpider.scale, Space.World);
  25. // Rotating the spider to camera forward
  26. transform.rotation = Quaternion.RotateTowards(transform.rotation, cameraLookRotation, Time.deltaTime * turnSpeed);
  27. }
  28. }
  29. }