using UnityEngine;
using System.Collections;
public class Shooter : MonoBehaviour {
public Rigidbody bullet;//刚体炮弹
public float power = 1500f;
public float moveSpeed = 2f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;//通过GetAxis来计算摄像机的移动距离
float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate(h, v, 0);//当前距离加上计算得出的移动距离
if (Input.GetButtonUp("Fire1"))
{
Rigidbody instance = (Rigidbody)Instantiate(bullet, transform.position, transform.rotation);//点击鼠标左键就产生一个刚体实例
Vector3 fwd = transform.TransformDirection(Vector3.forward);//面朝的方向作为力的方向
instance.AddForce(power * fwd);//施加力
}
}
}
Unity发射炮弹小例子源代码剖析
最新推荐文章于 2024-02-20 09:25:17 发布