You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复第一人称射击控制器中相机自动归位问题?

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

相机自动定位示意图

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 05:27:20