1)设置好相应场景
2)创建脚本挂载到相应物体上并编写
2.代码
1 //Shoot - - 控制小球生成与射击 2 3 using System.Collections; 4 using System.Collections.Generic; 5 using UnityEngine; 6 7 public class Shoot : MonoBehaviour { 8 9 public GameObject bullet; 10 public float speed=20; 11 12 13 // Update is called once per frame 14 void Update () { 15 16 //判断当鼠标左键按下时(左0,右1,中间2),实例化子弹 17 if (Input.GetMouseButtonDown(0)) 18 { 19 //实例化bullet上赋予的物体,并传递坐标和旋转 20 GameObject b= GameObject.Instantiate(bullet, transform.position, transform.rotation); 21 //声明并赋予rig刚体组建 22 Rigidbody rig = b.GetComponent<Rigidbody>(); 23 //赋予其一个向前的速度 24 rig.velocity = transform.forward * speed; 25 } 26 } 27 }
1 //Move Camera - - 控制摄像机的移动 2 3 using System.Collections; 4 using System.Collections.Generic; 5 using UnityEngine; 6 7 public class MoveCamera : MonoBehaviour { 8 9 public float speed = 5; 10 11 // Update is called once per frame 12 void Update () { 13 14 //获取当前坐标,h相当于左右,v相当于上下,具体是以vector3()设置在哪个坐标为主 15 //Horizontal和Vertical都是以1为单位,当移动/被调用一次后会归0,因此移动后才可以停止 16 //如果把vector3()某一坐标设为常数,运行游戏后就会一直移动 17 float h = Input.GetAxis("Horizontal"); 18 float v = Input.GetAxis("Vertical"); 19 20 //debug信息 21 Debug.Log(h); 22 23 //Time.deltaTime是时间增量,相当于每一帧所用时间(FPS由你的游戏实际情况为准) 24 //由于Update方法是每帧调用一次(帧为单位移动),乘以这个值可以理解为把他转换为以秒为单位,换句话说就是物体每秒移动多少米 25 //transform.Translate这个函数把坐标数值传递给当前script所挂载的物件 26 transform.Translate(new Vector3(h, v, 0) * Time.deltaTime* speed); 27 28 } 29 }