banner



How To Stop Third Person Camera From Clipping In Unity

A Third-Person photographic camera is a type of camera placed behind the player, usually slightly shifted to the side, giving a visual representation of the game level and the actor itself.

To make a Third-Person Shooter (TPS) photographic camera in Unity we volition use a combination of a regular thespian movement and a tertiary-person view.

Sharp Coder Video Player

Step 1: Create Histrion Controller

Commencement, we volition create a Histrion controller that will handle rotation and movement:

  • Create a new Game Object (Game Object -> Create Empty) and name it "Histrion"
  • Create new Sheathing (Game Object -> 3D Object -> Sheathing) and move it inside "Player" Object
  • Remove Sheathing Collider component from Capsule and change its position to (0, 1, 0)
  • Create a new GameObject and proper name it "CameraParent" and move it inside "Histrion" Object, alter its position to (0, ane.64, 0)
  • Move the Main Camera inside "CameraParent" Object and move it backside the Player (In my case I moved it to this position: (0.five, 0.6, -2.ix))

Third Person Camera in Unity

  • Create a new Script and call it SC_TPSController and paste the code beneath within it:

SC_TPSController.cs

          using UnityEngine;  [RequireComponent(typeof(CharacterController))]  public course SC_TPSController : MonoBehaviour {     public float speed = seven.5f;     public float jumpSpeed = 8.0f;     public float gravity = 20.0f;     public Transform playerCameraParent;     public float lookSpeed = 2.0f;     public bladder lookXLimit = 60.0f;      CharacterController characterController;     Vector3 moveDirection = Vector3.zero;     Vector2 rotation = Vector2.zero;      [HideInInspector]     public bool canMove = true;      void Commencement()     {         characterController = GetComponent<CharacterController>();         rotation.y = transform.eulerAngles.y;     }      void Update()     {         if (characterController.isGrounded)         {             // We are grounded, so recalculate move direction based on axes             Vector3 forward = transform.TransformDirection(Vector3.forward);             Vector3 right = transform.TransformDirection(Vector3.correct);             bladder curSpeedX = canMove ? speed * Input.GetAxis("Vertical") : 0;             float curSpeedY = canMove ? speed * Input.GetAxis("Horizontal") : 0;             moveDirection = (forward * curSpeedX) + (right * curSpeedY);              if (Input.GetButton("Jump") && canMove)             {                 moveDirection.y = jumpSpeed;             }         }          // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and one time below         // when the moveDirection is multiplied by deltaTime). This is because gravity should exist applied         // as an acceleration (ms^-2)         moveDirection.y -= gravity * Time.deltaTime;          // Move the controller         characterController.Movement(moveDirection * Time.deltaTime);          // Thespian and Camera rotation         if (canMove)         {             rotation.y += Input.GetAxis("Mouse X") * lookSpeed;             rotation.ten += -Input.GetAxis("Mouse Y") * lookSpeed;             rotation.10 = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit);             playerCameraParent.localRotation = Quaternion.Euler(rotation.x, 0, 0);             transform.eulerAngles = new Vector2(0, rotation.y);         }     } }        
  • Attach SC_TPSController script to "Thespian" Object (Y'all volition discover that it besides added some other component called Character Controller, change its center value to (0, 1, 0))
  • Assign "CameraParent" Object to playerCameraParent variable

Step 2: Add together Photographic camera Collision Detection

Camera collision detection will consist of a script that volition check if there is anything between the Camera and the Role player, and will automatically move the Camera closer, thus preventing the Camera from clipping through the objects.

  • Create a new script, name information technology SC_CameraCollision then paste the lawmaking beneath within it:

SC_CameraCollision.cs

          using UnityEngine;  public class SC_CameraCollision : MonoBehaviour {     public Transform referenceTransform;     public float collisionOffset = 0.3f; //To forestall Camera from clipping through Objects     public float cameraSpeed = 15f; //How fast the Camera should snap into position if there are no obstacles      Vector3 defaultPos;     Vector3 directionNormalized;     Transform parentTransform;     float defaultDistance;      // Kickoff is called before the first frame update     void Outset()     {         defaultPos = transform.localPosition;         directionNormalized = defaultPos.normalized;         parentTransform = transform.parent;         defaultDistance = Vector3.Distance(defaultPos, Vector3.zero);          //Lock cursor         Cursor.lockState = CursorLockMode.Locked;         Cursor.visible = imitation;     }      // LateUpdate is called after Update     void LateUpdate()     {         Vector3 currentPos = defaultPos;         RaycastHit striking;         Vector3 dirTmp = parentTransform.TransformPoint(defaultPos) - referenceTransform.position;         if (Physics.SphereCast(referenceTransform.position, collisionOffset, dirTmp, out hit, defaultDistance))         {             currentPos = (directionNormalized * (hitting.distance - collisionOffset));              transform.localPosition = currentPos;         }         else         {             transform.localPosition = Vector3.Lerp(transform.localPosition, currentPos, Fourth dimension.deltaTime * cameraSpeed);         }     } }        
  • Adhere SC_CameraCollision to Main Camera
  • Assign CameraParent to Reference Transform variable
  • Tweak Collision Showtime and Camera Speed values if the Camera is clipping through the walls

The TPS Photographic camera is now set up, press Play to test it.

Source: https://sharpcoderblog.com/blog/third-person-camera-in-unity-3d

Posted by: crewsmistne.blogspot.com

0 Response to "How To Stop Third Person Camera From Clipping In Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel