Question by
Gmr_Phorlyfe · Oct 06, 2021 at 11:02 PM ·
3rd person camera
My camera won't move with my player in 3rd person
The camera won't move no clue what's wrong. please help.
thank you for your time. :)
namespace MyRpg.MyCollections
{
public class CameraHandler : MonoBehaviour
{
#region Public variables
public Transform targetTransform;
public Transform cameraTransform;
public Transform cameraPivotTransform;
public static CameraHandler singleton;
public float lookSpeed = 0.1f;
public float followSpeed = 0.1f;
public float pivotSpeed = 0.03f;
public float minimumPivot = -35;
public float maximumPivot = 35;
#endregion
#region Private variables
private Transform myTransform;
private Vector3 cameraTransformPosition;
private LayerMask ignoreLayers;
private float defaultPosition;
private float lookAngle;
private float pivotAngle;
#endregion
#region functionality
private void Start()
{
singleton = this;
myTransform = transform;
defaultPosition = cameraTransform.localPosition.z;
ignoreLayers = ~(1 << 8 | 1 << 10);
}
public void FollowTarget(float delta)
{
Vector3 targetPosition = Vector3.Lerp(myTransform.position, targetTransform.position, delta / followSpeed);
myTransform.position = targetPosition;
}
public void HandleCameraRotation(float delta, float mouseXInput, float mouseYInput)
{
lookAngle += (mouseXInput = lookSpeed) / delta;
pivotAngle -= (mouseYInput = pivotSpeed) / delta;
pivotAngle = Mathf.Clamp(pivotAngle, minimumPivot, maximumPivot);
Vector3 rotation = Vector3.zero;
rotation.y = lookAngle;
Quaternion targetRotation = Quaternion.Euler(rotation);
myTransform.rotation = targetRotation;
rotation = Vector3.zero;
rotation.x = pivotAngle;
targetRotation = Quaternion.Euler(rotation);
cameraPivotTransform.localRotation = targetRotation;
}
#endregion
}
}
help.jpg
(118.7 kB)
Comment
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MyRpg.MyCollections
{
public class InputHandler : MonoBehaviour
{
public float horizontal;
public float vertical;
public float moveAmount;
public float mouseX;
public float mouseY;
PlayerControls inputActions;
CameraHandler cameraHandler;
Vector2 movementInput;
Vector2 cameraInput;
private void Awake()
{
cameraHandler = CameraHandler.singleton;
}
private void FixedUpdate()
{
float delta = Time.fixedDeltaTime;
if (cameraHandler != null)
{
Debug.Log("Should work");
cameraHandler.FollowTarget(delta);
cameraHandler.HandleCameraRotation(delta, mouseX, mouseY);
}
}
probably should have added this too from the beginning. sorry
the code is totally fine, the camera should be teleporting to the player position, whats exactly happening? doesnt move?
Answer by xxmariofer · Oct 07, 2021 at 06:25 AM
your code is missing an update or coroutine that calls the FollowTarget every frame?