ExampleWheelController.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. using UnityEngine;
  2. public class ExampleWheelController : MonoBehaviour
  3. {
  4. public float acceleration;
  5. public Renderer motionVectorRenderer; // Reference to the custom motion vector renderer
  6. Rigidbody m_Rigidbody;
  7. static class Uniforms
  8. {
  9. internal static readonly int _MotionAmount = Shader.PropertyToID("_MotionAmount");
  10. }
  11. void Start()
  12. {
  13. m_Rigidbody = GetComponent<Rigidbody>(); // Get reference to rigidbody
  14. m_Rigidbody.maxAngularVelocity = 100; // Set max velocity for rigidbody
  15. }
  16. void Update()
  17. {
  18. if (Input.GetKey (KeyCode.UpArrow)) // Rotate forward
  19. m_Rigidbody.AddRelativeTorque(new Vector3(-1 * acceleration, 0, 0), ForceMode.Acceleration); // Add forward torque to mesh
  20. else if (Input.GetKey (KeyCode.DownArrow)) // Rotate backward
  21. m_Rigidbody.AddRelativeTorque(new Vector3(1 * acceleration, 0, 0), ForceMode.Acceleration); // Add backward torque to mesh
  22. float m = -m_Rigidbody.angularVelocity.x / 100; // Calculate multiplier for motion vector texture
  23. if (motionVectorRenderer) // If the custom motion vector texture renderer exists
  24. motionVectorRenderer.material.SetFloat(Uniforms._MotionAmount, Mathf.Clamp(m, -0.25f, 0.25f)); // Set the multiplier on the renderer's material
  25. }
  26. }