操纵杆输入仅对角移动对象

时间:2014-03-29 19:16:32

标签: unity3d joystick

Unity中的单一操纵杆脚本用法存在问题。

我使用下面的脚本通过刚体移动切碎机。当我用箭头键统一测试游戏时,一切都按预期完美运行。但是,如果我在手机上测试,斩波器只能从左下角向右上方移动。

我确信这与我将操纵杆连接到X和Y轴的方式有关,但我没有发现我的错误......任何人都可以用一双新眼睛看着这个并告诉我在哪里搞砸了?

    #pragma strict

var speed : float = 500.0;
var smooth : float = 2.0;
var gravity : float = 0.0;
var moveJoystick : Joystick;


    function FixedUpdate () {

    var horPos = Input.GetAxis ("Horizontal") ? 
                   Input.GetAxis ("Horizontal") : joyStickInput(moveJoystick);
var verPos = Input.GetAxis ("Vertical") ? 
                 Input.GetAxis ("Vertical") : joyStickInput(moveJoystick);


    var movedirection = new Vector3(horPos, 0, verPos);

if (movedirection != Vector3.zero){
        var newRotation = Quaternion.LookRotation(movedirection * -1);
        transform.rotation = Quaternion.Slerp(transform.rotation, newRotation,     Time.deltaTime * smooth);
        }
    rigidbody.AddForce (movedirection * speed * Time.deltaTime);
     }

    function joyStickInput (joystick : Joystick) {
   var absJoyPos = Vector2 (Mathf.Abs(joystick.position.x),
                               Mathf.Abs(joystick.position.y));
   var xDirection = (joystick.position.x > 0) ? 1 : -1;
   var yDirection = (joystick.position.y > 0) ? 1 : -1;
   return ( ( absJoyPos.x > absJoyPos.y) ? absJoyPos.x * xDirection : absJoyPos.y * yDirection);
   }

1 个答案:

答案 0 :(得分:1)

因为horPos和verPos使用相同的joyStickInput(moveJoystick)?

按右键时,joyStickInput()将返回1 将horPos和verPos设置为相同的值(1),它将使您的精灵向右移动(y + 1)(x + 1)