hand
_1_7_7
4
返回栏目
1k
0.2k
0.5k
0.1k
0k
0.2k
0k
0.7k
0k
0k
0.4k
0.1k
4k
0.2k
0.1k
2k
0.2k
0.2k
0k
0k
0.5k
0k
0.9k
0.1k
1k
0.9k
2k
1k
2k
0.9k
1k
0k
1k
0k
1k
1k
1k
1k
0k
1k
1k
1k
0k
3k
1k
0k
0.3k
1k
0.3k
0.9k
0k
8k
9k
6k
2k
1k
2k
1k
1k
2k
0.6k
1k
0.6k
1k
0.5k
0.2k
2k
2k
2k
1k
2k
0k
0.8k
0.8k
1k
0k
0k
0k
0k
0k
0k
0k
0k
0.3k
0.4k
0.8k
0.3k
5k
1k
3k
7k
6k
3k
3k
1k
0.2k
2k
3k
2k
0.4k
0.5k
3k
2k
7k
3k
4k
2k
1k
6k
6k
1k
1k
0.9k
0.2k
0.3k
0.9k
1k
0k
0.8k
2k
0k
0.4k
0.1k
1k
0.9k
0.2k
1k
1k
0.5k
0k
0k
0.4k
4k
0.4k
2k
3k
1k
1k
1k
3k
5k
0k
1k
2k
0.5k
0k
1k
0k
1k
0.5k
0.8k
返回Unity - 游戏引擎栏目
作者:
贺及楼
成为作者
更新日期:2024-05-10 16:26:14
作用:发射子弹
其他方法:刚体移动之AddForce()函数
update — 鼠标点击 — 给加速度
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shoot : MonoBehaviour
{
public GameObject bullet;
public float speed = 5;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
GameObject b = GameObject.Instantiate(bullet,transform.position,transform.rotation);
Rigidbody rgd = b.GetComponent<Rigidbody>();
rgd.velocity = transform.forward * speed;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shoot : MonoBehaviour
{
public GameObject bullet;//发射的东西
public float speed = 5; //速度
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))//判断鼠标点击
{
GameObject b = GameObject.Instantiate(bullet,transform.position,transform.rotation);//实例化,位置在目标的位置,目标的旋转角度
Rigidbody rgd = b.GetComponent<Rigidbody>();//获得Rigidbody属性
rgd.velocity = transform.forward * speed;//设置Rigidbody属性的velocity属性
}
}
}
1.把脚本拉到要发射的物体
2.添加要发射子弹
3.注意命名空间这里是shoot
Unity - 游戏引擎
整章节共151节
快分享给你的小伙伴吧 ~