iLMS知識社群歷程檔登入
位置: 黃國哲 > Unity
1091229 免費的虛擬搖桿(失敗)
by 黃國哲 2020-12-29 11:04:48, 回應(0), 人氣(574)



https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631



////////////////////////////////////////////////////////////////////////


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Myscript : MonoBehaviour
{

protected Joystick joystick ;
protected Joybutton joybutton ;

protected bool jump ;


//Use for initialization

void start () 
{
joystick = FindObjectOfType<Joystick>();
joybutton = FindObjectOfType<Joybutton>();
}

//Update is called once per frame

void Update ()
{
var rigidbody = GetComponent<Rigidbody>();

rigidbody.velocity = new Vector3(joystick.Horizontal * 100f ,
rigidbody.velocity.y,
joystick.Vertical * 100f);

if(!jump && joybutton.Pressed)
{
jump = true ;
rigidbody.velocity += Vector3.up * 100f ;
}
if(jump && joybutton.Pressed)
{
jump = false ;
}
}
}
//////////////////////////////////////////////////////////////////////

using UnityEngine;
using UnityEngine.EventSystems;

public class Joybutton : MonoBehaviour , IPointerUpHandler , IPointerDownHandler 
{

[HideInInspector]
protected bool Pressed ;
//Use for initialization

void start () 
{}

//Update is called once per frame

void Update ()
{}

public void OnPointerDown(PointerEventData eventData)
{
Pressed = true ;
}

public void OnPointerUp(PointerEventData eventData)
{
Pressed = false ;
}
}













回應