C# 如何更改游戏对象';s旋转等于我的播放器摄像头';什么是轮换?

C# 如何更改游戏对象';s旋转等于我的播放器摄像头';什么是轮换?,c#,unity3d,C#,Unity3d,我在做一个第一人称射击游戏。我有一个瞄准功能,它把手枪放在摄像机的正前方,使它看起来像你拿着它在你面前。我试着让它这样,手枪也会随着相机在Z轴上旋转,这样手枪就不会静止不动了,因为这看起来很奇怪,而且会碍事。为此,我尝试了以下方法: GPR.gun.transform.rotation=Quaternion.Euler(0,0,plrCam.transform.rotation.z),但这最终会使枪绕z轴略微旋转,并且每当我移动相机时,主要是绕y轴旋转。我是Unity的初学者程序员,所以请尽量让

我在做一个第一人称射击游戏。我有一个瞄准功能,它把手枪放在摄像机的正前方,使它看起来像你拿着它在你面前。我试着让它这样,手枪也会随着相机在Z轴上旋转,这样手枪就不会静止不动了,因为这看起来很奇怪,而且会碍事。为此,我尝试了以下方法:
GPR.gun.transform.rotation=Quaternion.Euler(0,0,plrCam.transform.rotation.z),但这最终会使枪绕z轴略微旋转,并且每当我移动相机时,主要是绕y轴旋转。我是Unity的初学者程序员,所以请尽量让初学者更容易理解我的答案,这样我才能理解它。以下是我的完整脚本:

using System.Collections.Generic;
using UnityEngine;

public class PistolFire : MonoBehaviour
{
    //Gun Properties

    public float range = 50f;
    public float damage = 10f;
    //Sensitivity decrease for looking down the sights
    public float downSights = 5f;

    //Other vars
    private playerGunControls playerGun;
    private GameObject plrCam;
    private Camera fpsCam;
    private ParticleSystem muzzleFlash;
    private GameObject impactEffect;
    private bool aimed = false;
    private GameObject aimPos;
    private GunPickupRaycast GPR;
    private GameObject handPos;
    private GameObject Player;

    // Start is called before the first frame update
    void Start()
    {
        //Getting objects because gun is instantiated, so this is necessary
        plrCam = GameObject.Find("Player Camera");
        playerGun = plrCam.GetComponent<playerGunControls>();
        fpsCam = plrCam.GetComponent<Camera>();
        muzzleFlash = GetComponentInChildren<ParticleSystem>();
        impactEffect = GameObject.Find("Impact Effect");
        aimPos = GameObject.Find("aimPos");
        GPR = plrCam.GetComponent<GunPickupRaycast>();
        handPos = GameObject.Find("Hand Pos");
        Player = GameObject.Find("Player");
    }

    // Update is called once per frame
    void Update()
    {
        //Check for shoot button down
        if (Input.GetButtonDown("Fire1"))
        {
            if (playerGun.holding == "Pistol")
            {
                Shoot();
            }
        }
        //Check if aim button down
        if (Input.GetButton("Fire2"))
            {
            if (playerGun.holding == "Pistol")
            {
                Aim();
            }
        }
        //Check if no longer aiming to reset to normal
        if (aimed == true && !(Input.GetButton("Fire2")))
        {
            Unaim();
        }

    }
    void Shoot()
    {

        muzzleFlash.Play();

        RaycastHit hit;
        if(Physics.Raycast(plrCam.transform.position, plrCam.transform.forward, out hit, range))
        {
            Debug.Log(hit.transform.name);

            Health health = hit.transform.GetComponent<Health>();
            if (health != null)
            {
                health.TakeDamage(damage);
            }

            //Instantiate the Impact Effect
            GameObject IE = Instantiate(impactEffect, hit.point, Quaternion.identity);
            Destroy(IE, 1.5f);

        }
    }
    void Aim()
    {
        aimed = true;
        Debug.Log("Aiming");
        GPR.gun.transform.position = aimPos.transform.position;
        GPR.gun.transform.rotation = Quaternion.Euler(0, 0, plrCam.transform.rotation.z);
    }
    void Unaim()
    {
        GPR.gun.transform.position = handPos.transform.position;
        Debug.Log("No longer aiming");
        aimed = false;
    }
}
使用System.Collections.Generic;
使用UnityEngine;
公共级手枪射击:单一行为
{
//枪械性能
公共浮动范围=50f;
公众浮子损坏=10f;
//向下看时灵敏度降低
公共浮标下降高度=5f;
//其他变量
私人游戏者控制游戏者;
私有游戏对象plrCam;
私人摄像机;
私有粒子系统;
私人游戏对象冲击效应;
私有布尔=假;
私有游戏对象aimPos;
私人GunPickupRaycast探地雷达;
私有游戏对象handPos;
私人游戏对象玩家;
//在第一帧更新之前调用Start
void Start()
{
//获取对象,因为gun是实例化的,所以这是必要的
plrCam=GameObject.Find(“玩家相机”);
playerGun=plrCam.GetComponent();
fpsCam=plrCam.GetComponent();
muzzleFlash=getComponentChildren();
impactEffect=GameObject.Find(“冲击效果”);
aimPos=GameObject.Find(“aimPos”);
GPR=plrCam.GetComponent();
handPos=GameObject.Find(“handPos”);
Player=GameObject.Find(“Player”);
}
//每帧调用一次更新
无效更新()
{
//检查是否有击落按钮
if(Input.GetButtonDown(“Fire1”))
{
如果(playerGun.holding==“手枪”)
{
射击();
}
}
//检查瞄准按钮是否按下
if(Input.GetButton(“Fire2”))
{
如果(playerGun.holding==“手枪”)
{
Aim();
}
}
//检查是否不再瞄准重置为正常
if(aimmed==true&&!(Input.GetButton(“Fire2”))
{
Unaim();
}
}
空射()
{
口吻闪光。播放();
雷卡斯特击中;
if(物理光线投射(plrCam.transform.position,plrCam.transform.forward,out hit,range))
{
Log(hit.transform.name);
Health=hit.transform.GetComponent();
如果(运行状况!=null)
{
健康.伤害(损害);
}
//实例化影响效果
GameObject IE=实例化(冲击效果、命中点、四元数标识);
销毁(即1.5f);
}
}
无效目标()
{
目标=正确;
调试日志(“瞄准”);
GPR.gun.transform.position=aimPos.transform.position;
GPR.gun.transform.rotation=四元数.Euler(0,0,plrCam.transform.rotation.z);
}
void Unaim()
{
GPR.gun.transform.position=handPos.transform.position;
Log(“不再瞄准”);
目标=错误;
}
}

我解决了我的问题,把枪变成了我相机的孩子,而不是我播放器的孩子。

杰里米556:谢谢你自我回答你的问题,这对每个人都有好处。但是,如果你提供一些代码来解释你所做的事情,而不是简单地用文字来描述,那将是一个更好的答案。通过阅读以下资源,您可以大大改进您的答案: