靠近墙壁时如何向后移动枪支?

时间:2018-10-13 15:42:15

标签: unity3d

我正在统一制作FPS游戏。我编写了一个脚本,使枪支在靠近物体时向后移动,并且还启用了枪支瞄准镜。我已经使用了动画使枪向后移动(gunBackward)并启用范围(Scoper),还为枪的常规位置(idleGun)。但是现在当我玩游戏时,当我团结一致时,我无法在雷击中对撞机时启用瞄准镜。枪开始振动。我只能在查看天空框时启用范围。

public float distance;
public GameObject origin;
public bool scop;
public Animator anim;
public GameObject CrossHair;
public bool scoped;

// Use this for initialization
void Start () {
    anim = GetComponent<Animator>();
}

// Update is called once per frame
void Update () {
    if (Input.GetButtonDown ("Fire2")) {
        scoped = true;
        scop = false;
    } 
    if (Input.GetButtonUp ("Fire2"))
    {
        scoped = false;
        scop = true;
    }
    if (scoped == true) {
        anim.Play ("Scoper");
    }
        if (scoped == false)
    {
        anim.Play("idleGun");

    }

    Vector3 cartPos = new Vector3 (0.0f, 0.0f, 0.0f);
    RaycastHit hit;

    if (Physics.Raycast (cartPos+origin.transform.position, transform.TransformDirection (Vector3.forward), out hit)) 
    {
        distance = hit.distance;
    }
    if (hit.transform == null) {
        distance = 2.1f;
        return;
    }
    if (distance < 2) 
    {
        anim.Play ("gunBackward");
    } 
    if (distance > 2)
    {
        anim.Play("idleGun");
    }
}

}

0 个答案:

没有答案