前边,我们已经绘制了场景和可移动的玩家对象,现在我们给玩家对象加上武器,发射子弹
创建子弹准星
void OnGUI() {
int size = 12;
float posX = _camera.pixelWidth / 2 - size / 4;
float posY = _camera.pixelHeight / 2 - size / 2;
GUI.Label(new Rect(posX, posY, size, size), "*");
}
创建子弹
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PayShooter : MonoBehaviour {
private Camera _camera;
// Use this for initialization
void Start () {
_camera = GetComponent<Camera>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void OnGUI() {
int size = 12;
float posX = _camera.pixelWidth / 2 - size / 4;
float posY = _camera.pixelHeight / 2 - size / 2;
GUI.Label(new Rect(posX, posY, size, size), "*");
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
Vector3 point = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
Ray ray = _camera.ScreenPointToRay(point);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
StartCoroutine(SphereIndicator(hit.point));
}
}
}
private IEnumerator SphereIndicator(Vector3 pos) {
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = pos;
yield return new WaitForSeconds(1);
Destroy(sphere);
}
}
在下一篇,我们将创建敌人
游戏图示、游戏下载、源码下载http://blog.csdn.net/d276031034/article/details/56016801