Как в Unity создать собственную кнопку для управления плеером на мобильном устройстве ⇐ C#

Место общения программистов C#
Anonymous
Как в Unity создать собственную кнопку для управления плеером на мобильном устройстве

Сообщение Anonymous »

Моя цель — создать кнопку, которая будет управлять игроком в самолете для мобильных сенсорных устройств. Я создал кнопку и пытаюсь использовать ее для управления плеером, но у меня ничего не получается.
Вот скрипт C# моего плеера:

Код: Выделить всё

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
public float speed = 500f, jumpHeight = 500f;
Transform myTrans;
Rigidbody2D myBody;
Vector2 movement;//used as temp holder variable before applying to velocity
bool isGrounded = true;

void Start ()
{
myTrans = this.transform;
myBody = this.GetComponent() ;
}

void Update ()
{
//Check if the player is grounded or not
isGrounded = Physics2D.Linecast(myTrans.position,
GameObject.Find(this.name+"/tag_ground").transform.position,
LayerMask.NameToLayer("Player"));

//Just for visualization, not needed for gameplay
Debug.DrawLine (myTrans.position,
GameObject.Find(this.name+"/tag_ground").transform.position,
Color.red);

//Works for keyboards and joysticks
#if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT
Move (Input.GetAxisRaw("Horizontal"));
if (Input.GetButtonDown ("Jump"))
Jump ();
#endif
}
//
//Separate Move and Jump so they can be called externally by TouchButtons
//
public void Move(float horizontal_input)
{
movement = myBody.velocity;

if(isGrounded)//we can only move left and right if on the ground
movement.x = horizontal_input * speed * Time.deltaTime;

//Apply the movement to the player
myBody.velocity = movement;
}
public void Jump()//we can only jump if on the ground
{
if(isGrounded)
myBody.velocity += jumpHeight * Vector2.up * Time.deltaTime;
}
}
А вот скрипт сенсорной логики:

Код: Выделить всё

using UnityEngine;
using System.Collections;

public class TouchLogicV2 : MonoBehaviour
{
public static int currTouch = 0;//so other scripts can know what touch is currently on screen
[HideInInspector]
public int touch2Watch = 64;
private new GUITexture guiTexture;
public virtual void Update()//If your child class uses Update, you must call base.Update(); to get this functionality
{
//is there a touch on screen?
if(Input.touches.Length 

Подробнее здесь: [url]https://stackoverflow.com/questions/31277804/how-to-unity-create-your-own-button-to-control-the-player-on-mobile-device[/url]

Вернуться в «C#»