Hello guys, I started learning Unity today with help of the book: **Unity in Action** by *Joshep Hocking*.
The book concentrates on making an FPS game, so when I get to the part were I have to code the hit detection for the enemies, everytime that I type or copy/paste the example code to get a console log when the engine registers a hit on the enemies, I get the error in the title.
It's obvious that Joseph didn't make a mistake. I mean... I'm the stupid one here that's learning.
Anybody's willing to give me a hand? I'm having a blast learning C# and Unity.
Here's the code:
using UnityEngine;
using System.Collections;
public class Rayshoot : MonoBehaviour {
private Camera _camera;
void Start() {
_camera = GetComponent();
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), "*");
}
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)) {
GameObject hitObject = hit.transform.gameObject;
ReactiveTarget target = hitObject.GetComponent();
if (target != null) {
Debug.Log("Target hit");
} else {
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);
}
}
↧