unity3D---星球大战(一)
创建一个3D的球体,调整到合适大小。重置位置,为球体添加材质,直接进行拖拽就可以,将材质拖拽到scen中的球体上;
首先要解决的问题是,摄像机要随着场景的移动而移动;创建一个空物体作为父集,重置位置,将摄像机拖拽成他的子集;通过键盘左右键的输入来控制Y轴的旋转;达到选装摄像头的效果;
创建脚本RotateCamera挂载到空物体focal Point上;通过键盘左右键的输入来控制Y轴的旋转;达到选装摄像头的效果;
1 public class RotateCamera : MonoBehaviour 2 { 3 public float rotationSpeed;//转动的速度; 4 // Start is called before the first frame update 5 void Start() 6 { 7 8 } 9 10 // Update is called once per frame 11 void Update() 12 { 13 //按下左右键进行触发 14 float horizontalInput = Input.GetAxis("Horizontal"); 15 //移动的方向和移动的速度 16 transform.Rotate(Vector3.up, horizontalInput * rotationSpeed * Time.deltaTime); 17 } 18 }
上述实现了场景可以转动,接下来要实现player也要实现转动;
球体可以实现上下左右的旋转,并且可以跟着摄像机进行移动??
创建脚本挂载到player上,实现球体的前后移动,为了使小球能够前后移动,需要对小球添加一个刚体;
1 public class playerController : MonoBehaviour 2 { 3 private Rigidbody rb; 4 public float speed; 5 // Start is called before the first frame update 6 void Start() 7 { 8 rb = GetComponent<Rigidbody>(); 9 } 10 11 // Update is called once per frame 12 void Update() 13 { 14 //按下前后键进行触发 15 float forwardInput = Input.GetAxis("Vertical"); 16 //用刚体增加一个向前的力; 17 rb.AddForce(transform.forward*speed*forwardInput); 18 } 19 }
出现的问题,小球在滚动的时候,对应的XYZ坐标轴也发生了变化;并且小球向前运动的位置,已经不是朝向屏幕前方了;要实现一边转动屏幕,小球的运动方向还是屏幕面向的方向进行运动;我们会发现,Focal Point的XYZ轴的方向是不变的,所以我们可以让小球运动的方向沿着Focal Point的方向进行移动;
1 public class playerController : MonoBehaviour 2 { 3 private Rigidbody rb; 4 private GameObject focalPoint; 5 public float speed; 6 // Start is called before the first frame update 7 void Start() 8 { 9 rb = GetComponent<Rigidbody>(); 10 focalPoint = GameObject.Find("Focal Point"); 11 } 12 13 // Update is called once per frame 14 void Update() 15 { 16 //按下前后键进行触发 17 float forwardInput = Input.GetAxis("Vertical"); 18 //用刚体增加一个向前的力;向前的方向改成了Focal Point的方向; 19 rb.AddForce(focalPoint.transform.forward*speed*forwardInput); 20 } 21 }
创建敌人,敌人跟随着玩家,目的是把玩家赶下台子
再创建一个球体,对球体添加刚体,创建新的脚本进行挂载
1 public class Enemy : MonoBehaviour 2 { 3 private Rigidbody rb; 4 private GameObject player; 5 public float speed; 6 7 // Start is called before the first frame update 8 void Start() 9 { 10 rb = GetComponent<Rigidbody>(); 11 player = FindObjectOfType<playerController>().gameObject; 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 //敌人的冲力方向等于玩家的位置减去当前敌人的位置,normalized使之单位化 18 Vector3 playerDir = (player.transform.position - transform.position).normalized; 19 //对当前物体添加一个冲力,方向和速度相乘 20 rb.AddForce(playerDir*speed); 21 } 22 }
接下来实现两个小球相撞,添加弹力效果,也有点摩擦力的效果
为敌人小球添加物理材质,增加弹力和摩擦力;右击Assets,创建physics Material ,Bounciness改为1,Bounce Combine,改为Multiply,多个小球弹力作用在一起
在X,Z轴上随机位置生成三个小球;将enemy设置为预制件;
方法一:
1 public class SpawnManager : MonoBehaviour 2 { 3 public GameObject enemyPrefab; 4 //物体生成位置的边界值 5 public float spawnRange; 6 //随机生成的位置 7 public Vector3 randomPos; 8 // Start is called before the first frame update 9 void Start() 10 { 11 for (int i = 0; i < 3; i++) 12 { 13 RandomPos(); 14 //生成函数(复制生成的物体,位置,生成角度) 15 Instantiate(enemyPrefab, randomPos, Quaternion.identity); 16 } 17 18 } 19 20 // Update is called once per frame 21 void Update() 22 { 23 24 } 25 public void RandomPos() { 26 float spawnPosX = Random.Range(-spawnRange,spawnRange); 27 float spawnPosZ = Random.Range(-spawnRange,spawnRange); 28 randomPos = new Vector3(spawnPosX,0,spawnPosZ); 29 } 30 }
方法二:生成物体坐标位置的函数有返回值
1 public class SpawnManager : MonoBehaviour 2 { 3 public GameObject enemyPrefab; 4 //物体生成位置的边界值 5 public float spawnRange; 6 //随机生成的位置 7 // public Vector3 randomPos; 8 // Start is called before the first frame update 9 void Start() 10 { 11 for (int i = 0; i < 3; i++) 12 { 13 14 //生成函数(复制生成的物体,位置,生成角度) 15 Instantiate(enemyPrefab, RandomPos(), Quaternion.identity); 16 } 17 18 } 19 20 // Update is called once per frame 21 void Update() 22 { 23 24 } 25 //函数有返回值,将函数当作一个变量来使用; 26 public Vector3 RandomPos() { 27 float spawnPosX = Random.Range(-spawnRange,spawnRange); 28 float spawnPosZ = Random.Range(-spawnRange,spawnRange); 29 Vector3 randomPos = new Vector3(spawnPosX,0,spawnPosZ); 30 return randomPos; 31 } 32 }
小球吃掉buffer,增加力量;会弹开其余的敌人小球‘
在添加闪电buffer时,小球碰撞上会消失,所以需要添加碰撞体,但是此时buffer要当触发器,所以is Trigger要勾选上;注,buffer中不要添加刚体
1 public class playerController : MonoBehaviour 2 { 3 private Rigidbody rb; 4 private GameObject focalPoint; 5 public float speed; 6 public bool isPowerUp; 7 public float powerStrenth; 8 // Start is called before the first frame update 9 void Start() 10 { 11 rb = GetComponent<Rigidbody>(); 12 focalPoint = GameObject.Find("Focal Point"); 13 } 14 15 // Update is called once per frame 16 void Update() 17 { 18 //按下前后键进行触发 19 float forwardInput = Input.GetAxis("Vertical"); 20 //用刚体增加一个向前的力;向前的方向改成了Focal Point的方向; 21 rb.AddForce(focalPoint.transform.forward*speed*forwardInput); 22 } 23 //触发器,当遇到buffer时,能力加大,销毁buffer; 24 private void OnTriggerEnter(Collider other) 25 { 26 if (other.CompareTag("PowerUp")) { 27 isPowerUp = true; 28 Destroy(other.gameObject); 29 } 30 } 31 //能力加强之后,可以弹开敌人小球 32 private void OnCollisionEnter(Collision collision) 33 { 34 if (collision.gameObject.CompareTag("Enemy")&&isPowerUp) { 35 //获得敌人小球的刚体 36 Rigidbody enemyRb = collision.gameObject.GetComponent<Rigidbody>(); 37 //获得敌人小球弹开的方向; 38 Vector3 dirFromPlayer = collision.transform.position - transform.position; 39 //添加一个弹开的冲击力 40 enemyRb.AddForce(dirFromPlayer * powerStrenth, ForceMode.Impulse); 41 } 42 43 } 44 }

浙公网安备 33010602011771号