首页 文章

如何以45度的限制旋转我的播放器手的顶部和底部 . 团结5

提问于
浏览
0

我想在Unity中创建一个2D游戏 . 我的玩家有一些像涂鸦军队一样的功能 . 我想旋转我的玩家手和头部限制到45度上下像涂鸦军队玩家两边 . 我使用两个操纵杆进行移动,第二个用于瞄准目标 . 因此,第二个操纵杆可以在操纵杆移动的方向上旋转手和头部 . 我想将这种旋转限制在两侧上下45度(左和右(上下)) .

任何人都可以帮助我 .

这是我的小代码旋转2d精灵手臂和头部 .

using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;

public class PlayerRotation : MonoBehaviour {

    public float moveForce = 5, boostMultiplier = 2;
    GameObject ga;

    // Use this for initialization
    void Start () {
        ga = GameObject.Find ("Charac_Head");
    }

    // Update is called once per frame
    void FixedUpdate () {

        Vector2 moveVec = new Vector2 (CrossPlatformInputManager.GetAxis ("Horizontal2"), 
                              CrossPlatformInputManager.GetAxis ("Vertical2")) * moveForce;



        //ga.transform.LookAt(transform.position.toVector2() + moveVec);
            //ga.transform.Rotate (0, 0, moveVec.y);
        if (moveVec.y <= 4.0f && moveVec.y >= -4.0f) {
            //ga.transform.right = moveVec;
            ga.transform.Rotate(0,0,moveVec.y);
            Debug.Log (moveVec);
        }
        //}
    }

}

1 回答

  • 1

    您可以使用Mathf.Clamp在最小值和最大值之间强制执行最终值 .

    我没有测试代码,但我告诉你你需要什么:

    moveVec.y = Mathf.Clamp(moveVec.y + Time.deltaTime*rotateRate, -45.0f, 45.0f);
    transform.localEulerAngles = moveVec;
    

相关问题