我正在创建一个第一人称拍摄控制器。当我将相机定位到某个位置时,它会自动定位到角色的中间,移动它只会将其重置到默认位置。
我已经尝试移动相机,使其处于正确的位置,但是,它会自动纠正自己处于角色的中间。我已经尝试为相机创建一个序列化字段,以便手动分配相机。但是,这并没有解决这个问题。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class Shooter_Controller : MonoBehaviour
{
public GameObject bulletPrefab;
public Transform shootingPosition;
public Transform aimDownSightsPosition;
[SerializeField] private Camera mainCamera;
public float shootingRate = 0.1f;
public float bulletSpeed = 10f;
public float aimDownSightsSpeed = 5f;
public float defaultFov = 90f;
private float shootingTimer = 0f;
private bool isAiming = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
shootingTimer += Time.deltaTime;
//Shooting mechanic
if(Input.GetButtonDown("Fire1") && shootingTimer > shootingRate)
{
GameObject bullet = Instantiate(bulletPrefab, shootingPosition.position, Quaternion.LookRotation(mainCamera.transform.forward));
Rigidbody bulletRigidBody = bullet.GetComponent<Rigidbody>();
bulletRigidBody.velocity = mainCamera.transform.forward * bulletSpeed;
shootingTimer = 0f;
}
//Aiming mechanic
if (Input.GetButton("Fire2"))
{
isAiming = true;
}
else
{
isAiming = false;
}
if (isAiming)
{
mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, defaultFov / 2f, aimDownSightsSpeed * Time.deltaTime);
mainCamera.transform.localPosition = Vector3.Lerp(mainCamera.transform.localPosition, aimDownSightsPosition.localPosition, aimDownSightsSpeed * Time.deltaTime);
}
else
{
mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, defaultFov, aimDownSightsSpeed * Time.deltaTime);
mainCamera.transform.localPosition = Vector3.Lerp(mainCamera.transform.localPosition, Vector3.zero, aimDownSightsSpeed * Time.deltaTime);
}
}
}
1条答案
按热度按时间2admgd591#
问题出在这一行:
如果不瞄准,它会将相机的局部位置改为
Vector3.zero
,这是父对象的局部原点。