前邊,我們已經繪製了場景和可移動的玩家對象,現在我們給玩家對象加上武器,發射子彈
創建子彈準星
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