- Home /
Problems with Dot Product, hiding Minecraft like blocks, and Directions
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!
Answer by fafase · Jun 06, 2013 at 02:07 PM
I think the problem comes from the vector you use:
var playerVector = Vector3.Normalize(theCamera.transform.position - transform.forward);
I would rather go for a vector going from the object to the player "dotted" with the forward of the camera.
Maybe this will help you get the idea
http://www.youtube.com/watch?v=JAAGdF-Wdas&list=PLrlKq98BDhenhNh8G42dE0T-edvLvJ0ba∈dex=3
http://unitygems.com/basic-ai-character/#Sight
or even this one but the sound is really bad...http://www.youtube.com/watch?v=wthhwvkABxU
In all those examples, the idea is to have the NPC to see or not see you. The same is applied here. You could depending on the return value of the dot make some of the faces render and other not.
Thanks man! I didn't realize that it mattered which vector you put in first. I think I understand a lot better. Thanks a bunch!
Actually, in dot product it does not matter which you put first, it does in cross product since you get the opposite vector depending on the order. What mattered is that you passed a wrong vector. You needed Dot((object.position-cam.position).normalized,cam.forward)
Your answer
Follow this Question
Related Questions
Difference between Vector.up and transform.up 2 Answers
How to find an object directly above another w/out Raycast? 1 Answer
How precise is unity with it´s transforms? 2 Answers
How to apply a force in a specific direction in 2D ? 0 Answers
2 Objects Moving in the Same Direction, which is in front and behind? 1 Answer