如何修复第一人称射击控制器中相机自动归位问题?
第一人称射击控制器相机自动复位问题
- 制作第一人称射击控制器时,相机调整到指定位置后会自动回到角色中心,移动相机后也会重置到默认位置
- 已尝试手动调整相机至正确位置,但仍会自动修正到角色中心;也尝试创建序列化字段手动分配相机,问题未解决

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; //射击机制 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; } //瞄准机制 isAiming = Input.GetButton("Fire2"); 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); } } }
内容的提问来源于stack exchange,提问作者Daniel A
相关产品推荐
相关产品推荐

