TPS на Unity #2 - камера от третьего лица

preview_player
Показать описание
В данном видео мы сделаем камеру для нашего персонажа.
Приятного просмотра )
Если вам нравятся уроки и вы хотели бы поддержать автора, буду очень рад
Рекомендации по теме
Комментарии
Автор

Держите, сам сделал. Думаю вам будет нужно) Также она работает.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraHandler : MonoBehaviour
{
public Transform camTrans;
public Transform pivot;
public Transform Character;
public Transform mTransform;

public CharacterStatus characterStatus;
public CameraConfig cameraConfig;
public bool leftPivot;
public float delta;

public float mouseX;
public float mouseY;
public float smoothX;
public float smoothY;
public float smoothXVelocity;
public float smoothYVelocity;
public float lockAngle;
public float titlAngle;

void FixedUpdate()
{
FixedTick();

}

void FixedTick()
{
delta = Time.deltaTime;

HandlePosition();
HandleRotation();

Vector3 targetPosition = Vector3.Lerp(mTransform.position, Character.position, 1);
mTransform.position = targetPosition;
}

void HandlePosition()
{
float targetX = cameraConfig.normalX;
float targetY = cameraConfig.normalY;
float targetZ = cameraConfig.normalZ;

if (characterStatus.isAiming)
{
targetX = cameraConfig.aimX;
targetZ = cameraConfig.aimZ;
}

if(leftPivot)
{
targetX = -targetX;
}

Vector3 newPivotPosition = pivot.localPosition;
newPivotPosition.x = targetX;
newPivotPosition.y = targetY;

Vector3 newCameraPosition = camTrans.localPosition;
newCameraPosition.z = targetZ;

float t = delta * cameraConfig.pivotSpeed;
pivot.localPosition = Vector3.Lerp(pivot.localPosition, newPivotPosition, t);
camTrans.localPosition = Vector3.Lerp(camTrans.localPosition, newCameraPosition, t);
}

void HandleRotation()
{
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");

if(cameraConfig.turnSmooth > 0)
{
smoothX = Mathf.SmoothDamp(smoothX, mouseX, ref smoothXVelocity, cameraConfig.turnSmooth);
smoothY = Mathf.SmoothDamp(smoothY, mouseY, ref smoothXVelocity, cameraConfig.turnSmooth);
}
else
{
smoothX = mouseX;
smoothY = mouseY;
}

lockAngle += smoothX * cameraConfig.Y_rot_speed;
Quaternion targetRot = Quaternion.Euler(0, lockAngle, 0);
mTransform.rotation = targetRot;

titlAngle -= smoothY * cameraConfig.Y_rot_speed;
titlAngle = Mathf.Clamp(titlAngle, cameraConfig.minAngle, cameraConfig.maxAngle);
pivot.localRotation = Quaternion.Euler(titlAngle, 0, 0);
}
}

neliks
Автор

Спасибо за урок, кое-какие нюансы почерпал для себя).

deedel
Автор

Здравствуйте! Подскажите пожалуйста, почему в Create нет вкладки CharacterStatus?
Только Camera висит и всё 🤷🏻‍♂️
Код проверил и даже переписал на всякий случай - все вроде верно.

ВадимНест
Автор

Thanks for showing us Pro Third Person Camera!!

김승준-bc
Автор

ты молодец, надеюсь в новых видео нету такого гемора с камерой от третьего лица. ее можно было просто бросить на игрока и управлять камерой героем, соответственно и камера будет двигаться. лайк за работу конечно поставлю.

yamidvsolo
Автор

С кодом вроде все норм. Ошибок нет. Но камера просто постоянно крутится вокруг персонажа. В чем может быть проблема?

zloy_kak_tus
Автор

Я не могу камеру поднимать, только по х, а по у нет, помогии

kvtk_a
Автор

totally non understandable. what the fuckin u wana to do

MrPreetjee
welcome to shbcf.ru