【发布时间】:2020-01-17 18:13:13
【问题描述】:
我正在为 android 开发 2D 视频游戏,我唯一需要完成的就是制作触摸控件。视频游戏仅包含左右移动和跳跃(如 MarioBros)。我已经在画布上创建了按钮,我只需要脚本,非常感谢您的帮助,我附上我的播放器代码,以防您帮助我进行修改。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public float jumpPower = 15f;
private bool jump;
public bool grounded;
private Rigidbody2D rb2d;
private Animator anim;
private GameObject healthbar;
public GameObject panelGameOver;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
healthbar = GameObject.Find("Healthbar");
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
jump = true;
}
anim.SetBool("Grounded", grounded);
}
void FixedUpdate()
{
{
if (Input.GetKey(KeyCode.RightArrow))
{
if (GetComponent<SpriteRenderer>().flipX == true)
{
GetComponent<SpriteRenderer>().flipX = false;
}
GetComponent<Animator>().SetBool("Correr", true);
transform.Translate(0.1f, 0, 0);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
if (GetComponent<SpriteRenderer>().flipX == false)
{
GetComponent<SpriteRenderer>().flipX = true;
}
GetComponent<Animator>().SetBool("Correr", true);
transform.Translate(-0.1f, 0, 0);
}
if (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))
{
GetComponent<Animator>().SetBool("Correr", false);
}
//Saltar
if (jump)
{
rb2d.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
jump = false;
}
}
//
}
public void EnemyJump()
{
jump = true;
}
public void EnemyJumpB()
{
healthbar.SendMessage("TakeDamage", 30);
jump = true;
}
}
【问题讨论】:
标签: c# android unity3d touch game-engine