Hello. I’m to trying to create a very basic minecraft-esque game (which I’m starting to think of as the “Hello world” program of Unity) purely to further my understanding of C#. However, I’ve run into a problem. I’m trying to implement a feature where a faded out block appears that shows you where your block is going to be placed. I’m attempting to do this by instantiating a collider-less faded block, and then deleting it when you’re cursor moves away from that point. Unfortunately, before I’ve even really started, I’ve run into the problem that the block doesn’t instantiate at all. Code:
using UnityEngine;
using System.Collections;
public class Interaction : MonoBehaviour {
RaycastHit rayHit;
public Transform blockFaded;
private bool hasLooked;
void Start () {
hasLooked = false;
}
// Update is called once per frame
void Update () {
Vector3 fwd = transform.TransformDirection (Vector3.forward);
Vector3 spot = new Vector3(Mathf.Round (rayHit.point.x), Mathf.Ceil (rayHit.point.y),Mathf.Round (rayHit.point.z));
if(Physics.Raycast (transform.position, fwd, out rayHit, 5))
{
if (hasLooked==false)
{
Instantiate (blockFaded, spot, Quaternion.identity);
Debug.Log ("hit");
hasLooked = true;
}
}
}
}
Attached to a first person controller, this script should just place a single faded block on the ground and that’s it, but the block is never created. The Debug.Log, however, runs perfectly when it’s supposed to. Anybody know what’s going on?
Ah, thank you.
– PippyLongbeard