
作用:发射子弹
其他方法:刚体移动之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 updatevoid Start(){}// Update is called once per framevoid 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 updatevoid Start(){}// Update is called once per framevoid 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