我正在制作一款3d篮球游戏。
它有一个分数和一个计时器。在一段时间后,我的场景加载并从头开始。每次我投球的时候,我都要产卵。
它有一个产卵按钮和一个拍摄按钮。但我不想用产卵按钮。所以我想自动产卵这个球。
我怎么发动汽车呢?我给我的产卵按钮代码和抛出按钮代码下面。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnButton: MonoBehaviour
{
public GameObject ball;
public GameObject BallPosition;
public void Spawn()
{
ball.transform.position = BallPosition.transform.position;
var ballPosition = ball.transform.position;
ball.GetComponent<Rigidbody>().useGravity = false;
ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
ball.transform.position = ballPosition;
}
}using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowButton: MonoBehaviour
{
static Animator anim;
public GameObject ball;
public float ballThrowingForce = 5f;
internal bool holdingBall;
void Start()
{
anim = GetComponent<Animator>();
ball.GetComponent<Rigidbody>().useGravity = false;
}
public void Throw()
{
anim.SetTrigger("isThrowing");
StartCoroutine(Test());
}
IEnumerator Test()
{
yield return new WaitForSeconds(1.5f);
ball.GetComponent<Rigidbody>().useGravity = true;
//ball.GetComponent<Rigidbody>().AddForce(transform.up * ballThrowingForce);
ball.GetComponent<Rigidbody>().AddForce(0, 380.0f, ballThrowingForce);
}
}发布于 2019-12-16 00:13:00
要产生一个球,你应该创建一个预置并实例化它。示例:
private class Spawner : MonoBehaviour
{
public GameObject prefab;
public GameObject Spawn() => Instantiate(prefab);
}所以你的投掷代码应该产生一个球,如果你想销毁一个旧的球。
发布于 2019-12-16 02:03:45
Iv Misticos在上面的回答中提到了Instantiate的使用,这是一个很好的产生新球的方法。在繁殖新球之前,需要指定它必须繁殖的时间。
你也可以
虽然gameObject.active已经过时了,但它将在不使事情复杂化的情况下服务于该目的。在我看来,Invoke在这里比IEnumerator更好,因为Invoke不会停止执行流程,并在计时器运行时继续执行其他事情。
https://stackoverflow.com/questions/59343202
复制相似问题