FPSCharacter.cs 983 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.Demos {
  4. /// <summary>
  5. /// Demo character controller for the Full Body FPS scene.
  6. /// </summary>
  7. public class FPSCharacter: MonoBehaviour {
  8. [Range(0f, 1f)] public float walkSpeed = 0.5f;
  9. private float sVel;
  10. private Animator animator;
  11. private FPSAiming FPSAiming;
  12. void Start() {
  13. animator = GetComponent<Animator>();
  14. FPSAiming = GetComponent<FPSAiming>();
  15. }
  16. void Update() {
  17. // Aiming down the sight of the gun when RMB is down
  18. FPSAiming.sightWeight = Mathf.SmoothDamp(FPSAiming.sightWeight, (Input.GetMouseButton(1)? 1f: 0f), ref sVel, 0.1f);
  19. // Set to full values to optimize IK
  20. if (FPSAiming.sightWeight < 0.001f) FPSAiming.sightWeight = 0f;
  21. if (FPSAiming.sightWeight > 0.999f) FPSAiming.sightWeight = 1f;
  22. animator.SetFloat("Speed", walkSpeed);
  23. }
  24. void OnGUI() {
  25. GUI.Label(new Rect(Screen.width - 210, 10, 200, 25), "Hold RMB to aim down the sight");
  26. }
  27. }
  28. }