我正在用unity2d制作游戏,大多数机制都已实现。剩下的只是死后弹出的菜单。我已经创建了菜单,我只需要在死亡时显示它即可
我需要与敌人角色的动画,背景和敌人的动作发生碰撞。我不知道该怎么做。
我希望所有动画停止播放,游戏要暂停,并显示带有两个按钮的菜单a:重新启动并退出游戏。
玩家脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class HeroJumpScript : MonoBehaviour
{
//Player Script
private Animator anim;
private bool isGrounded = false;
private bool isDeath = false;
public Transform groundCheck;
public Transform EnemyCheck;
private float groundRadius = 1.0f;
private float enemyRadius = 4.0f;
public LayerMask WhatIsGround;
public Text ScoreText;
public float score = 0f;
public float verticalImpuls;
Rigidbody2D rb;
public bool IsRuning { get; set; }
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, WhatIsGround);
isDeath = Physics2D.OverlapCircle(EnemyCheck.position, enemyRadius, WhatIsGround);
anim.SetBool("Ground", isGrounded);
anim.SetBool("Enemy", isDeath);
anim.SetFloat("vSpeed", rb.velocity.y);
if (!isGrounded)
return;
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(new Vector2(0, verticalImpuls), ForceMode2D.Impulse);
}
}
private void OnCrash()
{
}
//reading score
public void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Enemy")
{
score++;
}
}
//jump
void Update()
{
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
rb.AddForce(new Vector2(0, 30));
}
ScoreText.text = "Score: " + score;
}
//implementation of the collision with the enemy and the ground conditional death
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
}
if (collision.gameObject.tag == "Enemy")
{
}
}
private void OnCollisionExit2D(Collision2D collis)
{
if (collis.gameObject.tag == "Ground")
{
}
}
}
敌人的移动脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyMove : MonoBehaviour
{
Rigidbody2D rb;
float speed = 4f;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
}
void Update ()
{
Vector2 vel = rb.velocity;
vel.x = -1 * speed;
rb.velocity = vel;
}
}
P.s。全文已在Google翻译器中翻译
答案 0 :(得分:0)
因此,您想在这里查看此方法:
private void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.tag == "Ground") {
}
if (collision.gameObject.tag == "Enemy") {
}
}
在第二个if语句上,您正在检查是否与敌人相撞。假设您希望玩家在碰到敌人时死亡,这是您要对死亡功能进行编程的地方。