Search Unity

(Solved) Third Person Camera, again....

Discussion in 'Scripting' started by RxGian, Jul 28, 2019.

  1. RxGian

    RxGian

    Joined:
    Mar 21, 2018
    Posts:
    82
    I want to make my own TPS style, I want to mimic gta san andreas which is smooth and never lags behind whatever how fast targeted move.

    I am not using Parent-child method because it makes me confused if I want to change target camera, everything input function I did it by script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InputHandler : MonoBehaviour
    6. {
    7.     public Camera camera;
    8.     public Animator mecAnim;
    9.    
    10.     void Awake()
    11.     {
    12.         if (camera == null)
    13.             camera = Camera.current;
    14.         if (mecAnim == null)
    15.             mecAnim = GameObject.FindObjectOfType<Animator>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         controlCharacterByRootMotion(mecAnim);
    22.         FollowCameraToObject(camera.transform, mecAnim.transform);
    23.     }
    24.  
    25.     public Vector3 offset = new Vector3(0, 1.25f, -3f);
    26.     Vector3 followPos;
    27.  
    28.     void FollowCameraToObject(Transform cameraTrans, Transform targetTrans)
    29.     {
    30.         targetTrans.Rotate(new Vector3 (Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")));
    31.  
    32.         followPos = (targetTrans.forward * offset.z)
    33.             + (targetTrans.right * offset.x)
    34.             + (targetTrans.up * offset.y)
    35.             + targetTrans.position;
    36.  
    37.         cameraTrans.position = followPos;
    38.         cameraTrans.LookAt(targetTrans.position + new Vector3 (offset.x, offset.y, targetTrans.forward.z));
    39.     }
    40.  
    41.     void controlCharacterByRootMotion(Animator animator)
    42.     {
    43.         animator.SetFloat("Straight", Input.GetAxis("Vertical"));
    44.         animator.SetFloat("Strafe", Input.GetAxis("Horizontal"));
    45.     }
    46. }
    47.  
    as you can see that I rotate targetTrans, but actually I don't want to do that. I just want to do orbit movement for camera. What should I do? sorry my code is complicated. Please use short and simple sentence so I can understand clearly.
     
  2. RxGian

    RxGian

    Joined:
    Mar 21, 2018
    Posts:
    82
    Wow this forum so helpful! once again, I've figured out myself even no one replies!
    Code (CSharp):
    1. void FollowCameraToObject(Transform cameraTrans, Transform targetTrans)
    2.     {
    3.         yaw += Input.GetAxis("Mouse X") * 10;
    4.         pitch -= Input.GetAxis("Mouse Y") * 10;
    5.         pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
    6.         Vector3 targetRotation = new Vector3(pitch, yaw);
    7.         cameraTrans.eulerAngles = targetRotation;
    8.         followPos = targetTrans.position + new Vector3(0, 1.75f,0);
    9.         cameraTrans.position = followPos + cameraTrans.TransformDirection(offset);
    10.     }
     
  3. Anizmo

    Anizmo

    Joined:
    Sep 4, 2015
    Posts:
    1
    Hey RxGian, your script helped me with the approach. Thank you for sharing, I am also adding a minYaw and maxYaw to stop the complete 360 rotation and make it more like the camera in GTA games. Thanks again!