这个简单的角色控制器可以左右移动我的平台游戏角色。
希望有人可以集成或向我展示如何为android / ios添加触控功能。
只需轻轻触摸屏幕左侧即可左右移动。
由于
using UnityEngine;
using System.Collections;
public class RobotController : MonoBehaviour {
//This will be our maximum speed as we will always be multiplying by 1
public float maxSpeed = 2f;
//a boolean value to represent whether we are facing left or not
bool facingRight = false;
//a value to represent our Animator
Animator anim;
// Use this for initialization
void Start () {
//set anim to our animator
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
float move = Input.GetAxis ("Horizontal");//Gives us of one if we are moving via the arrow keys
//move our Players rigidbody
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y);
//set our speed
anim.SetFloat ("Speed",Mathf.Abs (move));
//if we are moving left but not facing left flip, and vice versa
if (move < 0 && !facingRight) {
Flip ();
} else if (move > 0 && facingRight) {
Flip ();
}
}
//flip if needed
void Flip(){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
答案 0 :(得分:0)
在Update()方法中,您将要使用foreach循环,循环遍历该特定帧更新中的所有触摸事件。
foreach(Touch touch in Input.touches)
{
if(leftButton.HitTest(touch.position))
{
//move character left
}
if(rightButton.HitTest(touch.position))
{
//move character right
}
}
这假设您在start()方法中为左右控件设置了两个GUITexture按钮。