Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • TPS Camera with Wall Collisions in Unity

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.31k
    Comment on it

    Hi Nerds,
    Here is the tutorial for creating a third person camera with dynamic wall collisions in Unity.
    Demo: https://youtu.be/DPTWLs-zndI
    Doc: https://1drv.ms/p/s!AvAlkrGEujhegijtt9rN5vqZys_u
    Project: https://drive.google.com/open?id=0B9oh03OBnU50Ynh2ak1Rd2Z0Umc

     

    Code Example:

    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerCamera : MonoBehaviour {
    5.  
    6. public Transform targetTransform;
    7.  
    8. private Camera _Cam;
    9. public Camera Cam{
    10. get{
    11. if(_Cam == null){
    12. _Cam = GetComponent<Camera>();
    13. }
    14. return _Cam;
    15. }
    16. }
    17.  
    18. public bool isMoving;
    19.  
    20. public Vector3 CamOffset = Vector3.zero;
    21. public Vector3 ZoomOffset = Vector3.zero;
    22.  
    23. public float senstivityX = 5;
    24. public float senstivityY = 1;
    25.  
    26. public float minY = 30;
    27. public float maxY = 50;
    28.  
    29. public bool isZooming;
    30.  
    31. private float currentX = 0;
    32. private float currentY = 1;
    33.  
    34.  
    35. void Update(){
    36. currentX += Input.GetAxis("Mouse X");
    37. currentY -= Input.GetAxis("Mouse Y");
    38.  
    39. currentX = Mathf.Repeat(currentX, 360);
    40. currentY = Mathf.Clamp(currentY, minY, maxY);
    41.  
    42. isMoving = (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) ? true: false;
    43. isZooming = Input.GetMouseButton(1);
    44.  
    45.  
    46. if(isMoving || isZooming){
    47. UpdatePlayerRotation();
    48. }
    49.  
    50.  
    51. }
    52.  
    53.  
    54. void UpdatePlayerRotation(){
    55. targetTransform.rotation = Quaternion.Euler (0, currentX, 0);
    56.  
    57. }
    58.  
    59. void LateUpdate(){
    60.  
    61. Vector3 dist = isZooming? ZoomOffset : CamOffset;
    62.  
    63. Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
    64. transform.position = targetTransform.position + rotation * dist;
    65.  
    66. transform.LookAt(targetTransform.position);
    67.  
    68.  
    69. CheckWall();
    70. }
    71.  
    72.  
    73. public LayerMask wallLayer;
    74.  
    75. void CheckWall()
    76. {
    77.  
    78. RaycastHit hit;
    79.  
    80.  
    81. Vector3 start = targetTransform.position;
    82. Vector3 dir = transform.position - targetTransform.position;
    83.  
    84. float dist = CamOffset.z * -1;
    85. Debug.DrawRay(targetTransform.position, dir, Color.green);
    86. if(Physics.Raycast(targetTransform.position, dir, out hit, dist, wallLayer))
    87. {
    88. float hitDist = hit.distance;
    89. Vector3 sphereCastCenter = targetTransform.position + (dir.normalized * hitDist);
    90. transform.position = sphereCastCenter;
    91.  
    92. }
    93. }
    94.  
    95. }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: