C# 如何使PlayerController脚本使用触摸输入
如何使我的PlayerController脚本获得触摸输入,以使玩家在unity5中移动?我尝试将C# 如何使PlayerController脚本使用触摸输入,c#,unity3d,C#,Unity3d,如何使我的PlayerController脚本获得触摸输入,以使玩家在unity5中移动?我尝试将输入。GetKeyDown更改为GetMousedDown和GetTouch,但每次单击按钮时都没有任何反应,请帮助…提前感谢 using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { private static PlayerController instance;
输入。GetKeyDown
更改为GetMousedDown
和GetTouch
,但每次单击按钮时都没有任何反应,请帮助…提前感谢
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private static PlayerController instance;
public static PlayerController Instance
{
get
{
if(instance == null)
{
instance = GameObject.FindObjectOfType<PlayerController>();
}
return instance;
}
}
private Animator myAnimator;
[SerializeField]
private Transform bulletPosition;
[SerializeField]
private float movementSpeed;
private bool facingRight;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
public bool airControl;
[SerializeField]
private float jumpForce;
[SerializeField]
private GameObject bulletPrefab;
public Rigidbody2D MyRigidbody{ get; set;}
public bool Attack{ get; set;}
public bool Jump{ get; set;}
public bool OnGround{ get; set;}
// Use this for initialization
void Start ()
{
facingRight = true;
MyRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
HandleInput();
}
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
Debug.Log(horizontal);
OnGround = IsGrounded();
HandleMovement(horizontal);
Flip(horizontal);
HandleLayers();
}
private void HandleMovement(float horizontal)
{
if (MyRigidbody.velocity.y < 0)
{
myAnimator.SetBool("land", true);
}
if (!Attack && (OnGround || airControl))
{
MyRigidbody.velocity = new Vector2(horizontal * movementSpeed, MyRigidbody.velocity.y);
}
if (Jump && MyRigidbody.velocity.y == 0)
{
MyRigidbody.AddForce(new Vector2(0, jumpForce));
}
myAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
}
private void HandleInput()
{
if(Input.GetKeyDown(KeyCode.Space))
{
myAnimator.SetTrigger("jump");
}
if(Input.GetKeyDown(KeyCode.C))
{
myAnimator.SetTrigger("attack");
}
}
private void Flip(float horizontal)
{
if(horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private bool IsGrounded()
{
if(MyRigidbody.velocity.y <= 0)
{
foreach(Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for(int i = 0; i <colliders.Length; i++)
{
if(colliders[i].gameObject != gameObject)
{
return true;
}
}
}
}
return false;
}
private void HandleLayers()
{
if(!OnGround)
{
myAnimator.SetLayerWeight(1, 1);
}
else
{
myAnimator.SetLayerWeight(1, 0);
}
}
public void ShootBullet(int value)
{
if(!OnGround && value == 1 || OnGround && value == 0)
{
if (facingRight)
{
GameObject tmp = (GameObject)Instantiate(bulletPrefab, bulletPosition.position, Quaternion.Euler(new Vector3(0, 0, 0)));
tmp.GetComponent<Bullet>().Initialize(Vector2.right);
}
else
{
GameObject tmp = (GameObject)Instantiate(bulletPrefab, bulletPosition.position, Quaternion.Euler(new Vector3(0, 0, 180)));
tmp.GetComponent<Bullet>().Initialize(Vector2.left);
}
}
}
}
使用UnityEngine;
使用系统集合;
公共类玩家控制器:单行为{
私有静态PlayerController实例;
公共静态PlayerController实例
{
得到
{
if(实例==null)
{
instance=GameObject.FindObjectOfType();
}
返回实例;
}
}
私人动画师;
[序列化字段]
私人职位;
[序列化字段]
私人浮动移动速度;
私人楼宇朝向权;
[序列化字段]
私有转换[]基点;
[序列化字段]
私人浮动地面半径;
[序列化字段]
私人甲板码头;
[序列化字段]
公共场所空气控制;
[序列化字段]
私人部队;
[序列化字段]
私有游戏对象公告;
公共Rigidbody2D MyRigidbody{get;set;}
公共布尔攻击{get;set;}
公共bool跳转{get;set;}
公共bool OnGround{get;set;}
//用于初始化
无效开始()
{
facingRight=正确;
MyRigidbody=GetComponent();
myAnimator=GetComponent();
}
//每帧调用一次更新
无效更新()
{
HandleInput();
}
void FixedUpdate()
{
float horizontal=Input.GetAxis(“水平”);
Log(水平);
OnGround=isground();
手部运动(水平);
翻转(水平);
HandleLayers();
}
专用空心把手移动(水平浮动)
{
if(体速度y<0)
{
myAnimator.SetBool(“土地”,真实);
}
如果(!攻击和(空中控制)
{
MyRigidbody.velocity=新矢量2(水平*移动速度,MyRigidbody.velocity.y);
}
if(跳转(&y)body.velocity.y==0)
{
AddForce(新矢量2(0,jumpForce));
}
myAnimator.SetFloat(“速度”,Mathf.Abs(水平));
}
私有void HandleInput()
{
if(Input.GetKeyDown(KeyCode.Space))
{
myAnimator.SetTrigger(“跳跃”);
}
if(Input.GetKeyDown(KeyCode.C))
{
myAnimator.SetTrigger(“攻击”);
}
}
专用空心翻转(水平浮动)
{
如果(水平>0&&!朝向右侧| |水平<0&&朝向右侧)
{
facingRight=!facingRight;
Vector3 theScale=transform.localScale;
比例x*=-1;
transform.localScale=比例;
}
}
二等兵
{
if(MyRigidbody.velocity.y