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 infamouslyuseless · Apr 13, 2014 at 05:52 PM · javascriptobjectcheckfacing

[js] How To Check If The Player Is Facing An Object

In javascript , I would like to know how to check if the player if facing an object.

EG.

I have a cube, and I want this script to be on the cube

 if (player is facing cube)
 {
     //do action
 }

Does anyone know how I could do this?

Thank You.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Apr 13, 2014 at 05:55 PM

I don't know how you've structured your code (i.e. if the script will be on the player, the cube, or a 3rd object). A solution is to compare the angle between the forward of the player and a vector between the player and the cube. If it is below some threshold, it is facing the cube.

 var cubeDir = transform.position - player.position;
 var angle = Vector3.Angle(cubeDir, player.forward);
 
 if (angle < someValue) {
     Debug.Log("I'm facing the player");
 }
Comment
Add comment · Show 10 · 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 infamouslyuseless · Apr 13, 2014 at 05:57 PM 0
Share

The code would be on the cube, so could you edit the answer so it could be so that the script is on the cube?

avatar image robertbu · Apr 13, 2014 at 06:05 PM 0
Share

I edited the answer. It assumes Javascript and that you have an initialized variable called 'player' which is the transform of the Player.

avatar image infamouslyuseless · Apr 13, 2014 at 06:07 PM 0
Share

Thanks, but how would I know what the 'someValue' would be?

avatar image infamouslyuseless · Apr 13, 2014 at 06:13 PM 0
Share

Also, I'm sorry but how would I initialize the variable that find the transform of the player. I tried doing transform.Find but it gives me an error when I do that.

avatar image robertbu · Apr 13, 2014 at 06:20 PM 0
Share

Typically to initialize the player variable you would put this in the top of the file:

 var player : Transform;

Then you can drag and drop the Player game object onto the 'player' variable in the Inspector. Alternately you can do:

 private var player : Transform;
   
 function Start() {
     player = GameObject.Find("Player").transform;
 } 

Replace "Player" string in the Find() call with whatever you've named your Player.

As for 'someValue', replace it with whatever angle you consider facing. It is in degrees. Start with 45.0 and then adjust it. The smaller the angle, the more directly the player needs to be facing the cube to be considered facing.

Show more comments
avatar image
0

Answer by akauper · Apr 13, 2014 at 06:49 PM

Attach this script to your player game object. Use threshold to change the accuracy of facing a target

 var threshold : float = 0.3f;
 var target : GameObject;
 
 var dir : Vector3 = (target.transform.position - transform.position).normalized;
 var direction : Vector3 = Vector3.Dot(dir, transform.forward);
     
 if( direction >= threshold )
 {              
           // code here
 }
Comment
Add comment · 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
0

Answer by sami1592 · Apr 13, 2014 at 06:54 PM

This may not be the exact solution but this links below will lead to the solution. 10 mins and you are set :D

Raycasting Tutorials ---

First Tutorial

Second Tutorial

sorry, as I dont know their exact use i could not give the exact solution :(

Comment
Add comment · 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

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving an object 0 Answers

How does unity/javascript handle object references? 1 Answer

Saving Object Position in Float NOT Object 1 Answer

Cannot access sprite of a gameobject 1 Answer

Literal Notation Object Creation in unity? 0 Answers


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