我正在为PC创建一款游戏并尝试让它在Android上运行。但是我的一些C#代码出了问题。游戏在PC上工作正常,但是当我为android(apk)构建并将其传输到移动设备时,角色将无法移动。我只能使用火/射功能。以下是角色使用的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player: MonoBehaviour
{
private bool _faceRight;
private control_mainchar2D _control;
private float _normalizedxspeed;
public float speed_max = 4;
public float speedacceleration_onground = 10f;
public float speedacceleration_inair = 5f;
public float ShootRate;
public int Full_Health = 100;
public GameObject Damege_Effct;
public GameObject ShootProjectileEffects;
public Projectiles Projectiles;
public Transform ProjectilesShootDirection;
//Animator
public Animator Animation;
//Audio
public AudioClip Player_FireSfx;
public AudioClip Player_DamageSfx;
public AudioClip Player_HealSfx;
public int Health { get; private set; }
public bool Act_Dead { get; private set; }
public float _ableShootIn;
//private Collider2D _collider2D;
//public void Start()
//{
// _collider2D = GetComponent<Collider2D>();
//}
public void Awake()
{
_control = GetComponent<control_mainchar2D>();
_faceRight = transform.localScale.x > 0;
Health = Full_Health;
}
public void Update()
{
_ableShootIn -= Time.deltaTime;
if (!Act_Dead)
Control_Input();
var movementFactor = _control.State.IsGrounded ? speedacceleration_onground : speedacceleration_inair;
if (Act_Dead)
_control.Set_ForceX(0);
else
_control.Set_ForceX(Mathf.Lerp(_control.Velocity.x, _normalizedxspeed * speed_max, Time.deltaTime * movementFactor));
Animation.SetBool("ActLanded", _control.State.IsGrounded);
Animation.SetBool("ActDeath", Act_Dead);
Animation.SetFloat("Speed", Mathf.Abs(_control.Velocity.x) / speed_max);
}
public void FinishLevel()
{
enabled = false;
_control.enabled = false;
GetComponent<Collider2D>().enabled = false;
}
public void Death()
{
_control.Control_Collisions = false;
GetComponent<Collider2D>().enabled = false;
Act_Dead = true;
Health = 0;
_control.Set_Force(new Vector2(0, 20));
}
public void Respawn_Area(Transform spawn_Point)
{
if (!_faceRight)
Flip();
Act_Dead = false;
GetComponent<Collider2D>().enabled = true;
_control.Control_Collisions = true;
Health = Full_Health;
transform.position = spawn_Point.position;
}
public void Receive_Damage (int damage, GameObject instigator)
{
AudioSource.PlayClipAtPoint(Player_DamageSfx, transform.position);
TextFloat.Show(string.Format("-{0}", damage), "PlayerReceivedDamageText", new TextPositionerInGWPoint(Camera.main, transform.position, 2f, 60f));
Instantiate(Damege_Effct, transform.position, transform.rotation);
Health -= damage;
if (Health <= 0)
LevelManager.Instance.Player_Death();
}
public void AwardHealth(int health, GameObject instigator)
{
AudioSource.PlayClipAtPoint(Player_HealSfx, transform.position);
TextFloat.Show(string.Format("+ {0}!", health), "HealthAwardText", new TextPositionerInGWPoint(Camera.main, transform.position, 2f, 60f));
Health = Mathf.Min(Health + health, Full_Health);
}
private void Control_Input()
{
if (Input.GetKey(KeyCode.D))
{
_normalizedxspeed = 1;
if (!_faceRight)
Flip();
}
else if (Input.GetKey(KeyCode.A))
{
_normalizedxspeed = -1;
if (_faceRight)
Flip();
}
else
{
_normalizedxspeed = 0;
}
if (_control.Able_Skip && Input.GetKeyDown(KeyCode.Space))
{
_control.Skip();
}
if (Input.GetMouseButtonDown(0))
ShootProjectile();
if (Input.GetKey(KeyCode.F))
ShootProjectile();
}
private void ShootProjectile()
{
if (_ableShootIn > 0)
return;
if (ShootProjectileEffects != null)
{
var effect = (GameObject) Instantiate(ShootProjectileEffects, ProjectilesShootDirection.position, ProjectilesShootDirection.rotation);
effect.transform.parent = transform;
}
var direction = _faceRight ? Vector2.right : -Vector2.right;
var projectiles = (Projectiles)Instantiate(Projectiles, ProjectilesShootDirection.position, ProjectilesShootDirection.rotation);
projectiles.Initialize(gameObject, direction, _control.Velocity);
//projectiles.transform.localScale = new Vector3(_faceRight ? 1 : -1, 1, 1);
_ableShootIn = ShootRate;
AudioSource.PlayClipAtPoint(Player_FireSfx, transform.position);
Animation.SetTrigger("Shoot");
}
private void Flip()
{
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
_faceRight = transform.localScale.x > 0;
}
}
我是否需要修改整个代码才能使其在触摸屏移动设备/平板电脑上运行,或者我可以在Control_Input()方法下更改输入(如果是这样,我该如何处理)?
答案 0 :(得分:2)
您可以使用Platform #define directives
将您的Control_Input()
功能更改为此
#if UNITY_ANDROID
private void Control_Input()
{
if (Input.GetTouch(0).position.x > Screen.width / 2.0f) //If touched right part of screen
{
_normalizedxspeed = 1;
if (!_faceRight)
Flip();
}
else if (Input.GetTouch(0).position.x < Screen.width / 2.0f) //If touched left part of screen
{
_normalizedxspeed = -1;
if (_faceRight)
Flip();
}
else
{
_normalizedxspeed = 0;
}
if (_control.Able_Skip && Input.GetKeyDown(KeyCode.Space))
{
_control.Skip();
}
if (Input.GetMouseButtonDown(0))
ShootProjectile();
if (Input.GetKey(KeyCode.F))
ShootProjectile();
}
#else
private void Control_Input()
{
if (Input.GetKey(KeyCode.D))
{
_normalizedxspeed = 1;
if (!_faceRight)
Flip();
}
else if (Input.GetKey(KeyCode.A))
{
_normalizedxspeed = -1;
if (_faceRight)
Flip();
}
else
{
_normalizedxspeed = 0;
}
if (_control.Able_Skip && Input.GetKeyDown(KeyCode.Space))
{
_control.Skip();
}
if (Input.GetMouseButtonDown(0))
ShootProjectile();
if (Input.GetKey(KeyCode.F))
ShootProjectile();
}
#endif
Haven已经过测试,但我认为它应该可行。