Unity UGUI通过摇杆joystick 控制角色移动 标准的第三人称视角手游-左侧控制移动,右侧控制视角和方向

43 篇文章 1 订阅

Unity UGUI通过摇杆joystick 控制角色移动 标准的第三人称视角手游-左侧控制移动,右侧控制视角和方向

在这里插入图片描述

  • 代码实现

1,控制移动
  // Update is called once per frame
    void Update()
    {
        horizontal = analogLeft.localPosition.x;
        vertical = analogLeft.localPosition.y;

        //hor = 遥感脚本中的localPosition.x
        float hor = Horizontal;
        //hor = 遥感脚本中的localPosition.y
        float ver = Vertical;

        Vector3 direction = new Vector3(hor, 0, ver);

        if (direction != Vector3.zero)
        {
            //控制移动
            float newSpeed = Mathf.Lerp(ani.GetFloat("Speed"), 3, Time.deltaTime * 5);
            ani.SetFloat("Speed", newSpeed);
            //控制旋转
            ani.transform.rotation = Quaternion.Lerp(ani.transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * 10);
        }
        else
        {
            //停止移动
            float newSpeed = Mathf.Lerp(ani.GetFloat("Speed"), 0, Time.deltaTime * 5);
            ani.SetFloat("Speed", 0);
        }
    }

    /// <summary>
    /// 当鼠标开始拖拽时
    /// </summary>
    /// <param name="eventData"></param>
    public void OnDrag(PointerEventData eventData)
    {
        //获取鼠标位置与初始位置之间的向量
        Vector2 oppsitionVec = eventData.position - moveBackPos;
        //获取向量的长度
        float distance = Vector3.Magnitude(oppsitionVec);
        //最小值与最大值之间取半径
        float radius = Mathf.Clamp(distance, 0, maxRadius);
        //限制半径长度
        analogLeft.position = moveBackPos + oppsitionVec.normalized * radius;

    }
2,控制相机旋转
 void RotateAroundCube()
    {

        float x = 0;
        float y = 0;
#if  !UNITY_STANDALONE_WIN
        bool isright = false;
        foreach (var ta in Input.touches)
        {
            if (ta.position.x > (Screen.width / 2))
            {
                if (lastpoint != Vector3.zero)
                {
                    x = ta.position.x - lastpoint.x;
                    y = ta.position.y - lastpoint.y;
                }
                lastpoint = new Vector3(ta.position.x, ta.position.y);
                isright = true;
                break;
            }
        }
        if (Input.touches.Length <= 0)
        {
            lastpoint = Vector3.zero;
            x = 0;
            y = 0;
        }
        if (!isright)
        {
            lastpoint = Vector3.zero;
            x = 0;
            y = 0;
        }
#endif

#if UNITY_STANDALONE_WIN
        if (Input.GetMouseButton(0))
        {
            x = Input.GetAxis("Mouse X");
            y = Input.GetAxis("Mouse Y");
            rotateX += x * 2f;
            // Debug.Log("rotateX" + rotateX);
            rotateY -= y * 2f;
            // Debug.Log("rotateY" + rotateY);
        }
#else
            rotateX += x * 1f * Time.deltaTime;
            Debug.Log("rotateX" + rotateX);
            rotateY -= y * 1.1f * Time.deltaTime;
            Debug.Log("rotateY" + rotateY);
#endif
        //防止出现翻转现象
        rotateY = Mathf.Clamp(rotateY, -30, 35);
        //rotateZ += Input.GetAxis("Mouse ScrollWheel") * 5;
        //rotateZ = Mathf.Clamp(rotateZ, -12, -2);
        transform.position = target.transform.position +
             Quaternion.Euler(rotateY, rotateX, 0) * new Vector3(0, 1, rotateZ);
        transform.LookAt(target.transform);
        offset = transform.position - target.position;
        transform.position = offset + target.transform.position;
        
    }
}

在这里插入图片描述

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity中,我们可以通过编写代码实现左侧控制移动右侧控制视角方向的功能。首先,我们需要创建一个主角对象,并添加一个脚本来控制移动和旋转。 对于左侧移动控制,我们可以使用Unity的输入系统来获取玩家输入。可以使用Input.GetAxis函数获取玩家输入的水平(X轴)和垂直(Y轴)方向的值。然后,我们可以根据获取到的输入值调用主角对象的Translate函数来移动主角。 对于右侧视角方向控制,我们需要使用Unity的鼠标输入来获取玩家的鼠标移动。可以使用Input.GetAxis函数获取玩家鼠标在水平(X轴)和垂直(Y轴)方向上的移动值。然后,我们可以根据获取到的输入值调用主角对象的Rotate函数来旋转主角。 具体实现方案如下: ``` using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed = 5f; public float rotateSpeed = 100f; private void Update() { // 获取左侧控制输入 float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); // 根据输入移动主角 transform.Translate(horizontalInput * moveSpeed * Time.deltaTime, 0f, verticalInput * moveSpeed * Time.deltaTime); // 获取右侧控制输入 float mouseX = Input.GetAxis("Mouse X"); float mouseY = Input.GetAxis("Mouse Y"); // 根据鼠标输入旋转主角 transform.Rotate(Vector3.up * mouseX * rotateSpeed * Time.deltaTime); transform.Rotate(Vector3.right * -mouseY * rotateSpeed * Time.deltaTime); } } ``` 在这个实现中,我们假设水平输入被映射到了"Horizontal"轴,垂直输入被映射到了"Vertical"轴。同时,我们也假设鼠标水平输入被映射到了"Mouse X"轴,鼠标垂直输入被映射到了"Mouse Y"轴。 这样,通过编写上述代码,我们就可以实现Unity左侧控制移动右侧控制视角方向的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

unity_YTWJJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值