Моя задача сделать платформер на андроид. Все сделал по этому видео. ссылка на ютуб :
Основная проблема, в управлении под андроид. Хотелось бы сделать стрелки в движении право и лево , но не получается.
Использовал плагин CN Control персонаж двигается, но нет анимации. Помогите.....
Вот скрипт :
public class Character : Unit
{
Vector3 position;
[SerializeField]
private int lives = 5;
public int Lives
{
get { return lives; }
set
{
if (value < 5) lives = value;
livesBar.Refresh();
}
}
private LivesBar livesBar;
[SerializeField]
private float speed = 3.0F;
[SerializeField]
private float jumpForce = 15.0F;
private bool isGrounded = false;
private Bullet bullet;
private CharState State
{
get { return (CharState)animator.GetInteger("State"); }
set { animator.SetInteger("State", (int)value); }
}
new private Rigidbody2D rigidbody;
private Animator animator;
private SpriteRenderer sprite;
private void Awake()
{
livesBar = FindObjectOfType<LivesBar>();
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
bullet = Resources.Load<Bullet>("Bullet");
}
private void FixedUpdate()
{
CheckGround();
position = new Vector3(CnInputManager.GetAxis("Horizontal"), CnInputManager.GetAxis("Vertical"), 0f);
transform.position += position*Time.deltaTime*5;
if (position.x < 0)
{
transform.rotation = Quaternion.Euler(0, 180, 0);
}
else
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
// if (isGrounded )animator.SetBool("Run",true);
}
private void Update()
{
if (isGrounded) State = CharState.Idle;
///
// if (Input.GetButtonDown("Fire1")) Shoot();
/// if (Input.GetButton("Horizontal")) Run();
// if (isGrounded && Input.GetButtonDown("Jump")) Jump();
}
private void Run()
{
Vector3 direction = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
sprite.flipX = direction.x < 0.0F;
if (isGrounded) State = CharState.Run;
}
public void OnJump()
{
rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
if (isGrounded && Input.GetButtonDown("Jump")) Jump();
}
public void OnShoot()
{
Vector3 position = transform.position; position.y += 0.8F;
Bullet newBullet = Instantiate(bullet, position, bullet.transform.rotation) as Bullet;
newBullet.Parent = gameObject;
newBullet.Direction = newBullet.transform.right * (sprite.flipX ? -1.0F : 1.0F);
}
private void Jump()
{
rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
private void Shoot()
{
Vector3 position = transform.position; position.y += 0.8F;
Bullet newBullet = Instantiate(bullet, position, bullet.transform.rotation) as Bullet;
newBullet.Parent = gameObject;
newBullet.Direction = newBullet.transform.right * (sprite.flipX ? -1.0F : 1.0F);
}
public override void ReceiveDamage()
{
Lives--;
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(transform.up * 8.0F, ForceMode2D.Impulse);
Debug.Log(lives);
}
private void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.3F);
isGrounded = colliders.Length > 1;
if (!isGrounded) State = CharState.Jump;
}
private void OnTriggerEnter2D(Collider2D collider)
{
Bullet bullet = collider.gameObject.GetComponent<Bullet>();
if (bullet && bullet.Parent != gameObject)
{
ReceiveDamage();
}
}
}
public enum CharState
{
Idle,
Run,
Jump
}