欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

Unity3D實現(xiàn)虛擬按鈕控制人物移動效果

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

本文為大家分享了Unity3D實現(xiàn)虛擬按鈕控制人物移動的具體代碼,供大家參考,具體內容如下

創(chuàng)建Image的UI組件,在Image下新建一個Button按鈕。在Image 和Button上拖進Sprite圖片

在Button按鈕上掛載該腳本

using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
public class MyJoystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
 
  public Canvas canvas;
  public static float h;   //h和v的值傳回給player腳本,使得物體移動
  public static float v;
 
  private bool isPress = false; //Button按鈕是否按下
  private Vector2 touchPos = Vector2.zero; //按下的位置
 
  void Update() {
    if (isPress)
    {
      RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,
        Input.mousePosition, null, out touchPos);
 
      //根據(jù)Canvas和Image的Rectransform位置相減得出
      touchPos += new Vector2(427, 299); 
 
      float distance = Vector2.Distance(Vector2.zero, touchPos);
 
      if (distance > 52) { //限制Button不能超出Image位置(兩者位置相減得出52)
        touchPos = touchPos.normalized*52;
        transform.localPosition = touchPos;
      }
      else
      {
        transform.localPosition = touchPos;
      }
 
      h = touchPos.x / 52;
      v = touchPos.y / 52;
    }
 
  }
 
  //鼠標按下時觸發(fā)
  public void OnPointerDown(PointerEventData eventData) {
    isPress = true;
  }
 
  //鼠標按鍵彈起時觸發(fā)
  public void OnPointerUp(PointerEventData eventData)
  {
    isPress = false;
    transform.localPosition = Vector3.zero;
  }
 
}

在玩家身上掛載控制玩家移動的腳本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class PlayerMove : MonoBehaviour {
 
  public float speed = 0.1f;
 
  private float h = 0;
  private float v = 0;
 
  void Update() {
    //首先檢測虛擬按鍵有沒有移動,沒有再選擇鍵盤輸入
    if (Mathf.Abs(MyJoystick.h) > 0 || Mathf.Abs(MyJoystick.v) > 0) {
      h = MyJoystick.h;
      v = MyJoystick.v;
    }
    else{
      h = Input.GetAxis("Horizontal");
      v = Input.GetAxis("Vertical");
    }
    //玩家位置移動
    if (Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1) {
      Vector3 targetDir = new Vector3(h, 0, v);
      transform.position += targetDir * speed;
 
      transform.LookAt(transform.position+targetDir);
    }
    
  }
}

這樣,就能通過按下Button來控制玩家移動了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。

上一篇:UnityShader3實現(xiàn)2D描邊效果

欄    目:C#教程

下一篇:C#使用RichTextBox實現(xiàn)替換文字及改變字體顏色功能示例

本文標題:Unity3D實現(xiàn)虛擬按鈕控制人物移動效果

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4897.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有