Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Vector3.MoveTowards question with camera

Discussion in 'Scripting' started by AKing2713, Aug 19, 2020.

  1. AKing2713

    AKing2713

    Joined:
    Aug 16, 2020
    Posts:
    1
    I followed a tutorial for a 3rd person camera setup. You can find it here (not sure if I can post links)


    Long story short he sets up a follow system using an empty game object called "Camera Base" and then childs the camera to the game obj. He has another empty game object that is childed to our main character that is used in the script as an object for the camera base to follow. While both object's(Camera Base and Main Camera) transforms are zeroed out he backs the CAMERA away from the character changing the CAMERA'S local transform but not the transform of the parent. The script is on the Camera Base and makes the Camera Base move toward the follow object on the character. The thing is the Main Camera and Camera base don't move instantly towards the follow object like I would think it would. Instead it stays the desired amount back that we moved the camera to begin with. Only when the character moves does the camera react and follow. This is what I want to happen but is VERY confusing why it happens that way. If I zero out the camera base and main camera and move the CAMERA BASE backwards and run the script it would work how I think it would. The camera instantly moves toward the follow object and creates a sort of first person feel with the camera directly on top of the follow object.

    I guess my question is why does this happen? Why does affecting the transform of the child(Main Camera) make it so our camera base never reaches our intended target but instead stays back a desired amount away from the character. Here is the code associated. It is a bit different from the tutorial as I am using touch controls instead of mouse for the rotation but the movement part is the same

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraControl : MonoBehaviour
    6. {
    7.     public FixedTouchField TouchField;
    8.     public float CameraMoveSpeed = 120.0f;
    9.     public GameObject CameraFollowObj;
    10.     public float clampAngle = 80.0f;
    11.     public float inputSensitivity = 150.0f;
    12.     private float rotY = 0.0f;
    13.     private float rotX = 0.0f;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         Vector3 rot = transform.localRotation.eulerAngles;
    19.         rotY = rot.y;
    20.         rotX = rot.x;
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         //ROTATION
    27.         rotY += TouchField.TouchDist.x * Time.deltaTime * inputSensitivity;
    28.         rotX += TouchField.TouchDist.y * Time.deltaTime * inputSensitivity;
    29.  
    30.         rotX = Mathf.Clamp (rotX, -clampAngle, clampAngle);
    31.  
    32.         Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
    33.         transform.rotation = localRotation;
    34.     }
    35.     void LateUpdate()
    36.     {
    37.         CameraUpdater();
    38.     }
    39.  
    40.     void CameraUpdater()
    41.     {
    42.         //set obj target to follow
    43.         Transform target = CameraFollowObj.transform;
    44.  
    45.         //move towards the game obj that is the target
    46.         float step = CameraMoveSpeed * Time.deltaTime;
    47.         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    48.     }
    49. }
    50.  
     
  2. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    Code (CSharp):
    1. float step = CameraMoveSpeed * Time.deltaTime;
    This part allows for a slow movement towards the target. Its so if you are running the camera will lag behind the player. Then when you stop it will catch up and get to the desired distance behind the player. Some games use this as an ascetic.