繁体   English   中英

如何在突围中重生桨和球 - Unity 3D C#

[英]How to respawn a paddle and ball in breakout - Unity 3D C#

我正在 3D 中制作一款突破性游戏,这是我第一次制作游戏并使用 Unity,所以我有点无能为力。 我已经达到了我的游戏正常运行的地步,直到球离开屏幕并进入“死区”。 有人可以建议如何一起重生桨和球并继续游戏吗? 我在下面包含了我的球和桨脚本,我也有一个用于积木的脚本,但不确定是否相关。 我还制作了一个球和桨的预制件,但不知道如何处理它。 感谢任何能提供帮助的人:)


我的球的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallScript : MonoBehaviour
{
public Rigidbody rbody;

public float MinVertMovement = 0.1f;
public float MinSpeed = 10f;
public float MaxSpeed = 10f;
private bool hasBeenLaunched = false;

void Start()
{

}
private float minVelocity = 10f;
private Vector3 lastFrameVelocity;

void FixedUpdate()
{

  if (hasBeenLaunched == false)
  {
    if (Input.GetKey(KeyCode.Space))
    {
      Launch();
    }
  }

  if (hasBeenLaunched)
  {
    Vector3 direction = rbody.velocity;
    direction = direction.normalized;
    float speed = direction.magnitude;
    if (direction.y>-MinVertMovement && direction.y <MinVertMovement)
    {
      direction.y = direction.y < 0 ? -MinVertMovement : MinVertMovement;
      direction.x = direction.x < 0 ? -1 + MinVertMovement : 1 - MinVertMovement;
      rbody.velocity = direction * MinSpeed;
    }

    if (speed<MinSpeed || speed>MaxSpeed)
    {
      speed = Mathf.Clamp(speed, MinSpeed, MaxSpeed);
      rbody.velocity = direction*speed;
    }
  }
  lastFrameVelocity = rbody.velocity;

}

void OnCollisionEnter(Collision collision)
{

  Bounce(collision.contacts[0].normal);

}

private void Bounce(Vector3 collisionNormal)
{
  var speed = lastFrameVelocity.magnitude;
  var direction = Vector3.Reflect(lastFrameVelocity.normalized, collisionNormal);
  Debug.Log("Out Direction: " + direction);
  rbody.velocity = direction * Mathf.Max(speed, minVelocity);
}

public void Launch()
 {
    rbody = GetComponent<Rigidbody>();

    Vector3 randomDirection = new Vector3(-5f, 10f, 0);
    randomDirection = randomDirection.normalized * MinSpeed;
    rbody.velocity = randomDirection;
    transform.parent = null;
    hasBeenLaunched = true;
  }


}

我的桨的代码

public class PaddleScript : MonoBehaviour
{
private float moveSpeed = 15f;

void Start()
{

}

void Update()
{
    if (Input.GetKey(KeyCode.RightArrow) && transform.position.x<9.5)
      transform.Translate(moveSpeed *Input.GetAxis("Horizontal")*Time.deltaTime, 0f, 0f);
    if (Input.GetKey(KeyCode.LeftArrow) && transform.position.x>-7.5)
      transform.Translate(moveSpeed *Input.GetAxis("Horizontal")*Time.deltaTime, 0f, 0f);

}

}

检查球是否离开屏幕的最简单的方法是立即在相机周边放置一个触发器,并为你的球添加一个OnTriggerEnter2D方法。

/* Inside the ball script */

private void OnTriggerEnter() { // Use the 2D version if you're using 2D colliders
    /* Respawning stuff */
}

由于您可能希望在触发该方法时发生一堆不同的事情,因此您可能希望使用Unity Event ,这不是性能之王,但对于像breakout这样的游戏来说可能并不重要。

using UnityEngine.Events;

public class BallScript : MonoBehaviour
{
    public UnityEvent onBallOut;
    /* ... */

    private void OnTriggerEnter() {
        onBallOut.Invoke();
    }
}

然后,您可能需要一个 Respawn() 方法(不是 Reset(),因为这是默认的 MonoBehaviour 调用),它将球放回其原始 position,您可以在场景加载后立即将其存储在字段中:

private Vector3 defaultPosition;

private void Start() {
    defaultPosition = transform.position;
}

PS:如果你没有在你的 paddle 脚本中使用 Start() 方法,那么删除它,因为它会被 Unity 调用,即使它是空的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM