Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CG-DJ · Jun 06, 2013 at 01:46 PM · directionminecraftupdot

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CG-DJ · Jun 07, 2013 at 01:55 PM 0
Share

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!

avatar image fafase · Jun 08, 2013 at 07:35 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges