绑定在摄像机上的脚本
using UnityEngine;
using System.Collections;
public class abc : MonoBehaviour {
//设置移动速度
public int speed = 5;
//设置将被初始化加载的对象
public Transform newobject = null;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//通过左右方向键,或A、D字母键控制水平方向,实现往左、往右移动
float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
//通过上下方向键,或W、S字母键控制垂直方向,实现往前、往后移动
float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
//移动 绑定物的 x、z 轴,即移动 摄像机的 x、z 轴。
transform.Translate(x,0,z);
//判断是否按下鼠标的左键
if (Input.GetButtonDown("Fire1")) {
//实例化命令:Instantiate(要生成的物体, 生成的位置, 生成物体的旋转角度)
Transform n = (Transform)Instantiate(newobject, transform.position, transform.rotation);
//转换方向
Vector3 fwd = transform.TransformDirection(Vector3.forward);
//给物体添加力度
//Unity5之前的写法:n.rigidbody.AddForce(fwd * 2800);
n.GetComponent<Rigidbody>().AddForce(fwd * 2

这篇博客记录了Unity中如何实现发射小球并检测其与物体碰撞的代码细节,包括摄像机绑定的脚本和小球自身携带的脚本内容。
最低0.47元/天 解锁文章
6601

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



