unity虚拟摇杆的实现

在游戏中,虚拟摇杆是很常见的,这里我主要是制作第一人称的虚拟摇杆,第三人称的实现原理也是类似的。废话不说,直接说实现方式。
最终效果图如下:
虚拟摇杆
实现方式分为3步:
1:在canvas下创建一个空的子物体,名称为yangan或者你想要的其他名字,这里为yaogan。给子物体添加image组件,添加的方式是Component->UI->Image,给image组件绑定一个图片作为摇杆的背景图片。
2:创建一个Image物体,给image绑定一个图片做为摇杆的滑块,然后拖动Image物体到yaogan下作为摇杆的子物体。
3:代码的创建:
脚本1:挂到刚才创建的yaogan上,实现拖动事件和得到拖动偏移量。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class yaogan : ScrollRect
{
    public static bool ifmove=false;//目标是否移动
    protected float mRadius = 0f;
    float[] message = new float[2];
    private object endControl;

    protected override void Start()
    {
        base.Start();
        //计算摇杆块的半径
        mRadius = (transform as RectTransform).sizeDelta.x * 0.5f;
    }
//复写OnDrag方法
    public override void OnDrag(UnityEngine.EventSystems.PointerEventData eventData)
    {
        base.OnDrag(eventData);
        var contentPostion = this.content.anchoredPosition;
        if (contentPostion.magnitude > mRadius)
        {
            contentPostion = contentPostion.normalized * mRadius;
            SetContentAnchoredPosition(contentPostion);
            message[0] = contentPostion.x / contentPostion.magnitude;
            message[1] = contentPostion.y / contentPostion.magnitude;
            ifmove = true;
            GameObject.FindWithTag("MainCamera").SendMessage("GetValue",message);//这里我控制相机,所以给相机发送消息
           // Debug.Log(contentPostion.x/ contentPostion.magnitude+" Y"+ contentPostion.y / contentPostion.magnitude);
        }
    }
    //复写OnEndDrag方法
    public override void OnEndDrag(PointerEventData eventData)
    {
        base.OnEndDrag(eventData);
        ifmove = false;
    }
}
脚本2:挂到要控制的物体上,这里我控制的物体是maincamera.maincamera加了CharacterController组件。

using UnityEngine;
using UnityEngine.EventSystems;

public class shijiao : MonoBehaviour
{
public GameObject Cm;
//方向灵敏度
public float sensitivityX = 10F;
public float sensitivityY = 10F;

//上下最大视角(Y视角)
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
private float movespeed = 20;
CharacterController ctrlor;
float[] mesgs = new float[2];

void Start ()
{
  ctrlor = GetComponent<CharacterController>();
}
void Update()
{
    if (yaogan.ifmove)
    {
        Cm.transform.LookAt(new Vector3(Cm.transform.position.x+mesgs[0], Cm.transform.position.y,Cm.transform.position.z+mesgs[1]));
        ctrlor.Move(new Vector3(mesgs[0] * movespeed * Time.deltaTime, 0, mesgs[1] * movespeed * Time.deltaTime));
    }
}
void GetValue(float[] mesg)
{
    Debug.Log(mesg[0]);
    mesgs = mesg;
}

}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,针对您的问题,Unity实现虚拟摇杆可以通过以下步骤: 1. 创建一个空物体作为虚拟摇杆的父物体,命名为“Joystick”。 2. 在“Joystick”下创建两个子物体,一个是“Background”,一个是“Handle”。 3. 在“Background”下添加一个Image组件,用于显示虚拟摇杆的背景图。 4. 在“Handle”下添加一个Image组件,用于显示虚拟摇杆的手柄图。 5. 在“Joystick”上添加一个脚本,用于控制虚拟摇杆的行为。 以下是一个简单的虚拟摇杆脚本示例: ```csharp using UnityEngine; using UnityEngine.EventSystems; public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler { private RectTransform background; private RectTransform handle; private Vector2 inputVector; private void Start() { background = transform.GetChild(0).GetComponent<RectTransform>(); handle = transform.GetChild(1).GetComponent<RectTransform>(); } public virtual void OnDrag(PointerEventData eventData) { Vector2 pos; if (RectTransformUtility.ScreenPointToLocalPointInRectangle(background, eventData.position, eventData.pressEventCamera, out pos)) { pos.x = (pos.x / background.sizeDelta.x); pos.y = (pos.y / background.sizeDelta.y); inputVector = new Vector2(pos.x * 2 + 1, pos.y * 2 - 1); inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector; handle.anchoredPosition = new Vector2(inputVector.x * (background.sizeDelta.x / 3), inputVector.y * (background.sizeDelta.y / 3)); } } public virtual void OnPointerDown(PointerEventData eventData) { OnDrag(eventData); } public virtual void OnPointerUp(PointerEventData eventData) { inputVector = Vector2.zero; handle.anchoredPosition = Vector2.zero; } public float Horizontal() { if (inputVector.x != 0) return inputVector.x; else return Input.GetAxis("Horizontal"); } public float Vertical() { if (inputVector.y != 0) return inputVector.y; else return Input.GetAxis("Vertical"); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值