Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity C#摄像头故障_C#_Unity3d_Camera_Frame Rate_Rigid Bodies - Fatal编程技术网

Unity C#摄像头故障

Unity C#摄像头故障,c#,unity3d,camera,frame-rate,rigid-bodies,C#,Unity3d,Camera,Frame Rate,Rigid Bodies,我正在做一个FPS游戏,当我直接向右或向左看时,我的Z旋转设置为90,我的角色(胶囊)快速旋转。自从我把相机脚本添加到我的程序中以来,这种情况就一直在发生,所以我认为我添加的任何东西都与此无关 以下是我的相机移动代码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerCameraController : MonoBehaviour { [S

我正在做一个FPS游戏,当我直接向右或向左看时,我的Z旋转设置为90,我的角色(胶囊)快速旋转。自从我把相机脚本添加到我的程序中以来,这种情况就一直在发生,所以我认为我添加的任何东西都与此无关

以下是我的相机移动代码:

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

public class PlayerCameraController : MonoBehaviour
{
    [SerializeField] private float lookSensitivity;
    [SerializeField] private float smoothing;

    private GameObject player;
    private Vector2 smoothedVelocity;
    private Vector2 currentLookingPos;

    private void Start()
    {
        player = transform.parent.gameObject;
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = true;
    }

    private void Update()
    {
        RotateCamera();
    }

    private void RotateCamera()
    {
        Vector2 inputValues = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

        inputValues = Vector2.Scale(inputValues, new Vector2(lookSensitivity * smoothing, lookSensitivity * smoothing));

        smoothedVelocity.x = Mathf.Lerp(smoothedVelocity.x, inputValues.x, 1f / smoothing);
        smoothedVelocity.y = Mathf.Lerp(smoothedVelocity.y, inputValues.y, 1f / smoothing);

        currentLookingPos += smoothedVelocity;

        transform.localRotation = Quaternion.AngleAxis(-currentLookingPos.y, Vector3.right);
        player.transform.localRotation = Quaternion.AngleAxis(currentLookingPos.x, player.transform.up);
    }
}
以下是我的胶囊移动代码:

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

public class MovementController : MonoBehaviour
{
    [SerializeField] private float speed;
    [SerializeField] private float jumpForce;
    [SerializeField] private float jumpRaycastDistance;

    private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        Jump();
    }

    private void FixedUpdate()
    {
        Move();
    }

    private void Move()
    {
        float hAxis = Input.GetAxisRaw("Horizontal");
        float vAxis = Input.GetAxisRaw("Vertical");

        Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.fixedDeltaTime;

        Vector3 newPosition = rb.position + rb.transform.TransformDirection(movement);

        rb.MovePosition(newPosition);
    }
    private void Jump()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (IsGrounded())
            {
                rb.AddForce(0, jumpForce, 0, ForceMode.Impulse);
            }
        }
    }

    private bool IsGrounded()
    {
        return Physics.Raycast(transform.position, Vector3.down, jumpRaycastDistance);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类移动控制器:单行为
{
[字段]专用浮动速度;
[Field]私人浮动跳线部队;
[SerializeField]专用浮点数距离;
私人刚体;
私有void Start()
{
rb=GetComponent();
}
私有void更新()
{
跳跃();
}
私有void FixedUpdate()
{
Move();
}
私人空位移动()
{
float hAxis=Input.GetAxisRaw(“水平”);
float vAxis=Input.GetAxisRaw(“垂直”);
Vector3移动=新Vector3(hAxis,0,vAxis)*速度*时间。固定时间;
Vector3 newPosition=rb.position+rb.transform.TransformDirection(移动);
rb.移动位置(新位置);
}
私有无效跳转()
{
if(Input.GetKeyDown(KeyCode.Space))
{
如果(isground())
{
rb.附加力(0,跳跃力,0,力模式脉冲);
}
}
}
二等兵
{
返回Physics.Raycast(transform.position,Vector3.down,jumpraycast-distance);
}
}
以下是我的拍摄程序的代码:

using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

public class ShootController : MonoBehaviour
{

    [SerializeField] private Gun currentGun;

    private Transform cameraTransform;
    private float distance;
    private float damage;
    private float nextTimeToFire = 0f;
    private float burstTime = 0f;
    private int onBurst = 0;
    private int onPellet = 0;

    private void Start()
    {
        cameraTransform = Camera.main.transform;
    }

    private void Update()
    {
        CheckForShooting();
    }
    private void CheckForShooting()
    {
        if (currentGun.fireType == "Semi")
        {
            if (Input.GetMouseButtonDown(0))
            {
                RaycastHit whatIHit;
                if (Physics.Raycast(cameraTransform.position, transform.forward, out whatIHit, Mathf.Infinity))
                {
                    IDamagable damagable = whatIHit.collider.GetComponent<IDamagable>();
                    if (damagable != null)
                    {
                        distance = whatIHit.distance;
                        CalculateDamage();
                        damagable.DealDamage(Mathf.RoundToInt(damage));
                    }
                }
            }
        }
        if ((currentGun.fireType == "Auto") && Time.time >= nextTimeToFire)
        {
            if (Input.GetMouseButton(0))
            {
                RaycastHit whatIHit;
                if (Physics.Raycast(cameraTransform.position, transform.forward, out whatIHit, Mathf.Infinity))
                {
                    IDamagable damagable = whatIHit.collider.GetComponent<IDamagable>();
                    if (damagable != null)
                    {
                        distance = whatIHit.distance;
                        CalculateDamage();
                        damagable.DealDamage(Mathf.RoundToInt(damage));
                        nextTimeToFire = Time.time + 1/currentGun.fireRate;
                    }
                }
            }
        }
        if ((currentGun.fireType == "Single") && Time.time >= nextTimeToFire)
        {
            if (Input.GetMouseButtonDown(0))
            {
                RaycastHit whatIHit;
                if (Physics.Raycast(cameraTransform.position, transform.forward, out whatIHit, Mathf.Infinity))
                {
                    IDamagable damagable = whatIHit.collider.GetComponent<IDamagable>();
                    if (damagable != null)
                    {
                        distance = whatIHit.distance;
                        CalculateDamage();
                        damagable.DealDamage(Mathf.RoundToInt(damage));
                        nextTimeToFire = Time.time + 1 / currentGun.fireRate;
                    }
                }
            }
        }
        if ((currentGun.fireType == "Burst") && Time.time >= nextTimeToFire)
        {
            if (Input.GetMouseButtonDown(0))
            {
                while(onBurst < currentGun.roundBurst && Time.time >= burstTime) {
                    RaycastHit whatIHit;
                    if (Physics.Raycast(cameraTransform.position, transform.forward, out whatIHit, Mathf.Infinity))
                    {
                        IDamagable damagable = whatIHit.collider.GetComponent<IDamagable>();
                        if (damagable != null)
                        {
                            distance = whatIHit.distance;
                            CalculateDamage();
                            damagable.DealDamage(Mathf.RoundToInt(damage));
                            nextTimeToFire = Time.time + 1 / currentGun.fireRate;
                        }
                    }
                    burstTime = Time.time + 1 / 4;
                    onBurst++;
                }
                onBurst = 0;
            }
        }
        if ((currentGun.fireType == "Shotgun") && Time.time >= nextTimeToFire)
        {
            if (Input.GetMouseButtonDown(0))
            {
                while (onPellet < currentGun.pellets) { 
                    RaycastHit whatIHit;
                    Vector3 direction = transform.forward; 
                    Vector3 spread = new Vector3();
                    spread += transform.up * Random.Range(-10f, 10f); 
                    spread += transform.right * Random.Range(-10f, 10f); 

                    direction += Vector3.Normalize(spread) * Random.Range(0f, 0.2f);
                    if (Physics.Raycast(cameraTransform.position, direction, out whatIHit, Mathf.Infinity))
                    {
                        IDamagable damagable = whatIHit.collider.GetComponent<IDamagable>();
                        if (damagable != null)
                        {
                            distance = whatIHit.distance;
                            CalculateDamage();
                            damagable.DealDamage(Mathf.RoundToInt(damage)); 
                        }
                    }
                    onPellet++;
                }
                nextTimeToFire = Time.time + 1 / currentGun.fireRate;
            }
            onPellet = 0;
        }
    }
    private void CalculateDamage()
    {
        damage = (currentGun.maxDamage - currentGun.minDamage)/currentGun.minDamage*-distance+currentGun.maxDamage;
        if (damage > currentGun.maxDamage)
        {
            damage = currentGun.maxDamage;
        }
        if (damage < currentGun.minDamage)
        {
            damage = currentGun.minDamage;
        }
        Debug.Log(damage);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用System.Threading.Tasks;
使用UnityEngine;
公共类控制器:单行为
{
[Field]私家枪械;
私有转换;
私人浮动距离;
私人浮标损害;
私家花车火灾=0f;
专用浮点脉冲时间=0f;
私有int onBurst=0;
私有整数=0;
私有void Start()
{
cameraTransform=Camera.main.transform;
}
私有void更新()
{
CheckForShooting();
}
私有void CheckForShooting()
{
如果(currentGun.fireType==“半”)
{
if(Input.GetMouseButtonDown(0))
{
雷卡斯特·威蒂希特;
if(物理.光线投射(摄影机变换.位置,变换.前进,出什么,数学无穷))
{
IDamagable damagable=whatIHit.collider.GetComponent();
如果(可损坏!=null)
{
距离=whatIHit.distance;
计算图像();
可损坏的;可造成损坏的(损坏);
}
}
}
}
如果((currentGun.fireType==“自动”)&&Time.Time>=nextTimeToFire)
{
if(输入。GetMouseButton(0))
{
雷卡斯特·威蒂希特;
if(物理.光线投射(摄影机变换.位置,变换.前进,出什么,数学无穷))
{
IDamagable damagable=whatIHit.collider.GetComponent();
如果(可损坏!=null)
{
距离=whatIHit.distance;
计算图像();
可损坏的;可造成损坏的(损坏);
nextTimeToFire=Time.Time+1/currentGun.fireRate;
}
}
}
}
如果((currentGun.fireType==“Single”)&&Time.Time>=nextTimeToFire)
{
if(Input.GetMouseButtonDown(0))
{
雷卡斯特·威蒂希特;
if(物理.光线投射(摄影机变换.位置,变换.前进,出什么,数学无穷))
{
IDamagable damagable=whatIHit.collider.GetComponent();
如果(可损坏!=null)
{
距离=whatIHit.distance;
计算图像();
可损坏的;可造成损坏的(损坏);
nextTimeToFire=Time.Time+1/currentGun.fireRate;
}
}
}
}
如果((currentGun.fireType==“Burst”)&&Time.Time>=nextTimeToFire)
{
if(Input.GetMouseButtonDown(0))
{
while(onBurst=burstTime){
雷卡斯特·威蒂希特;
if(物理.光线投射(摄影机变换.位置,变换.前进,出什么,数学无穷))
{
IDamagable damagable=whatIHit.collider.GetComponent();
如果(可损坏!=null)
{
距离=whatIHit.distance;
计算图像();
可损坏的;可造成损坏的(损坏);
nextTimeToFire=Time.Time+1/currentGun.fireRate;
}
}
burstTime=Time.Time+1/4;
onBurst++;
}
onBurst=0;
}
}
如果((currentGun.fireType==“鸟枪”)&&Time.Time>=nextTimeToFire)
{
if(Input.GetMouseButtonDown(0))
{
而(onPellet