Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CameraController : MonoBehaviour {
  6.  
  7. Transform player;
  8. Animator playerAnimator;
  9. Vector3 targetPosition;
  10.  
  11. public Vector3 followOffset, idleOffset;
  12. public float maxCamSpeed;
  13.  
  14. // Use this for initialization
  15. void Start ()
  16. {
  17. player = GameObject.FindGameObjectWithTag("Player").transform;
  18. playerAnimator = player.GetComponent<Animator>();
  19. transform.position = player.position - player.forward * idleOffset.z + player.up * idleOffset.y;
  20. }
  21.  
  22. // Update is called once per frame
  23. void Update ()
  24. {
  25. if(playerAnimator.GetBool("isWalking"))
  26. {
  27. targetPosition = player.position - player.forward * followOffset.z + player.up * followOffset.y;
  28. }
  29. else
  30. {
  31. targetPosition = player.position - player.forward * idleOffset.z + player.up * idleOffset.y;
  32. }
  33.  
  34. if ((targetPosition - transform.position).magnitude > maxCamSpeed)
  35. {
  36. transform.position += (targetPosition - transform.position).normalized * maxCamSpeed;
  37. }
  38. else
  39. {
  40. transform.position = targetPosition;
  41. }
  42.  
  43. transform.LookAt(player.position);
  44.  
  45. transform.position += (targetPosition - transform.position).normalized * maxCamSpeed;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement