我的枪根据鼠标移动而移动,但在发射子弹时也会在其轴上旋转为什么?

时间:2016-01-14 15:36:13

标签: unity3d unityscript

当我按左键单击以拍摄子弹并表示同时(当鼠标左键仍然被点击时)我向右移动枪并让我的枪继续射击子弹但是也开始绕其原点旋转? 但是,如果我不点击鼠标左键并移动枪,它不会绕其轴旋转......

var clickPos    : Vector2;
var offsetPos   : Vector2;
var divider     = 80;

function Start()
{
    clickPos = Vector2(0,0);
    offsetPos = Vector2(0,0);
}

function Update () {

    offsetPos = Vector2(0,0);

    if(Input.GetKeyDown(leftClick()))
    {
        clickPos = mouseXY();
    }

    if(Input.GetKey(leftClick()))
    {
        offsetPos = clickPos - mouseXY();
    }


    transform.Rotate(Vector3(-(offsetPos.y/divider),offsetPos.x/divider,0.0), Space.World);
}

// Prints the current mouse position
function OnGUI ()
{
    /*GUI.Label(Rect(10,350,200,100), "mouse X = " + Input.mousePosition.x);
    GUI.Label(Rect(10,370,200,100), "mouse Y = " + Input.mousePosition.y);

    GUI.Label(Rect(120,350,200,100), "click X = " + clickPos.x);
    GUI.Label(Rect(120,370,200,100), "click Y = " + clickPos.y);

    GUI.Label(Rect(210,350,200,100), "offset X = " + offsetPos.x);
    GUI.Label(Rect(210,370,200,100), "offset Y = " + offsetPos.y);*/
}



//////////////////////////////////////////////

// Return true when left mouse is clicked or hold
function leftClick()
{
    return KeyCode.Mouse0;
}

//Immediate location of the mouse
function mouseXY()
{
    return Vector2(Input.mousePosition.x, Input.mousePosition.y);
}

//Immediate location of the mouse's X coordinate
function mouseX()
{
    return Input.mousePosition.x;
}

//Immediate location of the mouse's Y coordinate
function mouseY()
{
    return Input.mousePosition.y;
}

1 个答案:

答案 0 :(得分:0)

我得到了答案 答案是 评论偏移值,代码中使用的每个位置。 保持代码简单并注释或消除代码中的旋转线,这样就不会对喷枪执行任何旋转动作,只有位置在运动时发生变化。