Ok, so I’ve been fiddling with this on and off for a week…and I need help.
I’m doing an exercise of building a MineCraft world (no, I’m not making a MineCraft clone). So I read that, to keep the frame rate running smoothly, I should hide the 3 back faces the player can’t see. So I wrote this script to hide the back faces on a cube made of 6 planes.
#pragma strict
var playerObj : GameObject;
var theCamera : GameObject;
function Start ()
{
playerObj = GameObject.FindGameObjectWithTag("Player");
theCamera = GameObject.FindGameObjectWithTag("MainCamera");
}
function Update () {
var playerVector = Vector3.Normalize(theCamera.transform.position - transform.forward);
var faceVector = Vector3.Normalize(gameObject.transform.up);
if(Vector3.Dot(playerVector, faceVector) < 0)
{
renderer.enabled = false;
}
if(Vector3.Dot(playerVector, faceVector) > 0)
{
renderer.enabled = true;
}
}
(P.S It’s in UnityScript)
So this is my first time using Dot Products, and I just barely grasp the concept. So it’s kinda hard to figure out where this is going wrong. This script works perfectly for the 4 faces on the sides, but the top and bottom faces don’t work. They’re Mesh Renderer will be on when the plane is pointed toward the up, but not when the plane is pointed down. This is no matter where the cube is in relation to the player. Even if the cube is way above the player, the bottom face (which is pointed down) has it’s mesh renderer off, and the top face (which is pointed up), has its mesh renderer on.
WHAT IS GOING ON!!! Thanks for the help!