枪不要跟随相机

时间:2019-07-11 17:49:34

标签: c# unity3d

旋转手枪时位置播放器发生改变

Neutral camera & gun rotation

camera & gun rotated upwards

camera & gun rotated even more upwards

武器的位置:

Weapon's Position

克隆枪后武器的位置:

Weapon's Position  after cloning the gun:

主摄像机位置

Main Camera's position

相机的位置。相机是空的

Cameras's position

玩家:

hierarchy

如果查看图像,您会看到中心移动并且武器进行了自转。 我知道我不太了解,但是我想了解这是否是问题以及如何解决。 我已经陷入僵局了将近5天,因为我可以用另一种方式来做,但是我想了解出什么问题了, 谢谢。

代码:

Script

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

public class RuotaAttornoAlPersonaggio : MonoBehaviour
{
    public static bool cursorLocked=true;
    public Transform player;
    public Transform cams;
    public Transform weapon;

    public float xSens;
    public float ySens;
    public float maxAngle;

    private Quaternion camCenter;

    void Start()
    {
        camCenter = cams.localRotation;
    }

    // Update is called once per frame
    void Update()
    {
        SetY();
        SetX();
        UpdateCursorLock();
    }


    void SetY()
    {
        float y_input = Input.GetAxis("Mouse Y") * ySens * Time.deltaTime;
        Quaternion ty = Quaternion.AngleAxis(y_input, -Vector3.right);
        // Debug.Log(t);
        Quaternion ty_delta = cams.localRotation * ty;

        if (Quaternion.Angle(camCenter, ty_delta) < maxAngle)
        {
            cams.localRotation = ty_delta;
            weapon.localRotation = ty_delta;
        }

        // weapon.rotation = cams.rotatioùn;
    }

    void SetX()
    {
        float x_input = Input.GetAxis("Mouse X") * ySens * Time.deltaTime;
        Quaternion tx = Quaternion.AngleAxis(x_input, Vector3.up);
        // Debug.Log(t);
        Quaternion tx_delta = player.localRotation * tx;
        player.localRotation = tx_delta;
    }

    void UpdateCursorLock()
    {
        if (cursorLocked)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;

            if (Input.GetKeyDown(KeyCode.Escape))
            {
                cursorLocked = false;
            }
        }
        else
        {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;

            if (Input.GetKeyDown(KeyCode.Escape))
            {
                cursorLocked = true;
            }
        }
    }
}

代码克隆手枪: 我不认为这是代码的错误

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

public class Movimento : MonoBehaviour {
    public GameObject[] loadout;
    public Transform weaponParent;
    private GameObject curretWeapon;
    // Use this for initialization
    void Start () 
    {

    }

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetKeyDown(KeyCode.Alpha1)) Equip(0);

    }
    void Equip(int n)
    {
        if (curretWeapon != null) Destroy(curretWeapon);
        GameObject new_weap = (GameObject)Instantiate(loadout[n], weaponParent.position, weaponParent.rotation, weaponParent);
        new_weap.transform.localPosition = Vector3.zero;
        new_weap.transform.localEulerAngles = Vector3.zero;
        curretWeapon = new_weap;
    }
}

当我克隆枪时,武器的位置会改变。也许问题是当我旋转喷枪时,手枪是否以角色的位置为轴?我认为这是对象之间的关系问题。 你有什么想法? 谢谢

0 个答案:

没有答案