控制子弹运动的C#脚本为:
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
public float speed;// 公有变量,在inspector中赋值;设置为20,因为非常速度是10,子弹要比飞船快点嘛
// Update is called once per frame
void Start() {
// 子弹的运动
// 观察Scene视图中的坐标轴的位置,子弹需要朝着Z轴运动
// Vector3 transform.up即朝着z轴运动。 The GREEN axis(z轴) of the transform in world space
// 和飞船运动不同的是,飞船运动时借助键盘的水平和垂直方向键;transform.forward由Unity控制,并由人判断是否运动
// 下面直接运动,是为了做测试用,所以没加入判断
GetComponent<Rigidbody>().velocity = transform.up * speed;
}
}
本文介绍了一个使用C#编写的Unity游戏开发中控制子弹运动的脚本。该脚本通过设置刚体组件的velocity属性使子弹沿Z轴方向移动,实现子弹发射效果。
9126

被折叠的 条评论
为什么被折叠?



