各位大神,怎么无限克隆物体呀?
unity3d吧
全部回复
仅看楼主
level 4
rigidbodyuse 楼主
我本想做一个射击游戏,让克隆的子弹子弹射出后一秒自动销毁,结果发射了几颗子弹后,就射不出来了。
有没有大神可以帮帮我,真的谢谢了[太开心][太开心][太开心]
2022年10月03日 14点10分 1
level 4
rigidbodyuse 楼主
子弹发射代码:
using UnityEngine;
using System.Collections;
using System.Threading;
public class shoot : MonoBehaviour
{
void Start()
{
}
public class Stuff
{
public int bullets;
public int grenades;
public int rockets;
public Stuff(int bul, int gre, int roc)
{
bullets = bul;
grenades = gre;
rockets = roc;
}
}
public Stuff myStuff = new Stuff(10, 7, 25);
public float speed;
public float turnSpeed;
public Rigidbody bulletPrefab;
public Transform firePosition;
public float bulletSpeed;
void Update ()
{
Movement();
Shoot();
bulletSpeed = 40f;
}
void Movement ()
{
float forwardMovement = Input.GetAxis("Vertical") * speed * Time.deltaTime;
float turnMovement = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
transform.Translate(网页链接 * forwardMovement);
transform.Rotate(网页链接 * turnMovement);
}
void Shoot ()
{
if(Input.GetButtonDown("Fire1") && 网页链接 > 0)
{
Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, firePosition.rotation) as Rigidbody;
bulletInstance.AddForce(网页链接 * bulletSpeed);
myStuff.bullets--;
}
}
}
2022年10月03日 14点10分 2
level 4
rigidbodyuse 楼主
预制体销毁代码:
using UnityEngine;
using System.Collections;
public class yuzhiti2 : MonoBehaviour
{
void Start ()
{
}
void Update()
{
Destroy (gameObject,1);
}
void OnTriggerEnter( Collider GetObj)
{
if (GetObj != null)
Destroy (gameObject);
}
}
2022年10月03日 14点10分 3
level 4
rigidbodyuse 楼主
有人吗
2022年10月03日 15点10分 4
level 1
我也学习没多久,你还是截图好点,而且发射子弹还是比较适合用对象池,
2022年10月03日 15点10分 6
level 1
可以一开始就生成count个子弹预制体,都进入方法
Queue<子弹预制体> 池中子弹 = new();
Int 最大子弹量;
入队(Gameobject go){
if(池中子弹.count >= 最大子弹量){
删除(go);
}else 池中子弹.入队(go);
}
Gameobject 出队(){
Gameobject current=null;
if(池中子弹.count >= 0){
current = 池中中单.Dequeue();
current.设置可见方式(true);
return current;
}
return Instantiation(子弹预制体);
}
2022年10月03日 15点10分 8
然后你还得写回收部分的代码,
2022年10月03日 15点10分
谢谢了
2022年10月03日 23点10分
可以说说入队、出队、删除(go)、池中中单与设置可见方式怎么写吗?我是新手,问题有点多,请各位大佬谅解
2022年10月04日 00点10分
@rigidbodyuse b站有很多教程,可以去看看m_studio up主什么的
2022年10月04日 00点10分
level 1
我刚刚写了个,也挺像对象池的,可以参考参考
2022年10月04日 16点10分 9
这效果也很简单,就是 添加一个音乐,如果播放池中的播放器都在播放就在创建一个播放器。如果有播放器未播放就使用这个播放器,主要就是: 有就用,没就创建
2022年10月04日 16点10分
@西💢♂ 这个脚本启动方法就是pubilc修饰符的方法
2022年10月04日 16点10分
1