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 eeveelution8 · Feb 14, 2015 at 07:42 PM · camerarotationenemycamera rotate2.5d

2.5D enemy-player tracking script does not work.

Because I don't know how to use the vector.dot thing, I made a roundabout method.

There is a secondary part of an enemy that watches the player, and will change the direction of it's parent depending on where they are. the direction is based upon it's own rotation.

 if(transform.rotation.y >= 0){
     if(transform.rotation.y < 45){
     direction = 1;
 }}
 else if(transform.rotation.y >= 45){
     if(transform.rotation.y < 135){
     direction = 2;
 }}
 else if(transform.rotation.y >= 135){
     if(transform.rotation.y < 225){
     direction = 3;
 }}
 else if(transform.rotation.y >= 225){
     if(transform.rotation.y < 315){
     direction = 4;
 }}
 else if(transform.rotation.y >= 315){
     if(transform.rotation.y < 360){
     direction = 1;
 }}
 
 
 if(direction == 1){
 parent.renderer.material.mainTexture = tex1;
 }if(direction == 2){
 parent.renderer.material.mainTexture = tex2;
 }if(direction == 3){
 parent.renderer.material.mainTexture = tex3;
 }if(direction == 4){
 parent.renderer.material.mainTexture = tex4;
 }


No matter where the player goes, the direction Stays at 1, despite the transform's rotation in the inspector changing depending on where the player is.

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
1
Best Answer

Answer by Phantomized · Feb 14, 2015 at 09:36 PM

When you are using "else if" it will only enter that conditional statement if the preceding related "if" statement was false. Since "transform.rotation.y >= 0" is always true, all the "else if" statements will be ignored. You could instead use:

if (transform.eulerAngles.y >= 0 && transform.eulerAngles.y < 45) etcetera..

edit on my answer: Use transform.eulerAngles.y instead in this context, as that will return the angle in degrees.

Comment
Add comment · Show 5 · 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 eeveelution8 · Feb 14, 2015 at 10:58 PM 0
Share

tried changing it to

 if(transform.rotation.y >= 0 && transform.rotation.y < 45){
 direction = 1;
 }else if(transform.rotation.y >= 45 && transform.rotation.y < 135){
 direction = 2;
 }else if(transform.rotation.y >= 135 && transform.rotation.y < 225){
 direction = 3;
 }else if(transform.rotation.y >= 225 && transform.rotation.y < 315){
 direction = 4;
 }else if(transform.rotation.y >= 315 && transform.rotation.y < 360){
 direction = 1;
 }
 


still stays at 1.

avatar image Phantomized · Feb 15, 2015 at 12:17 AM 1
Share

Ah! Sorry I didn't even think about the fact that you used transform.rotation. That function will return a value between 0 and 1 based on your rotation. Use transform.eulerAngles.y ins$$anonymous$$d to get them in degrees.

avatar image eeveelution8 · Feb 15, 2015 at 07:22 AM 0
Share

I should put the full scripts I'm using, because the eulerangles doesn't work either.

the updated script is as follows,

 var daPlayer : GameObject;
 var parent : GameObject;
 
 var direction : int = 0;
 
 var tex1 : Texture2D;
 var tex2 : Texture2D;
 var tex3 : Texture2D;
 var tex4 : Texture2D;
 
 
 function Start(){
 daPlayer = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera");
 }
 
 function Update() {
 
  if(transform.eulerAngles.y >= 0 && transform.eulerAngles.y < 45){
  direction = 1;
  }else if(transform.eulerAngles.y >= 45 && transform.eulerAngles.y < 135){
  direction = 2;
  }else if(transform.eulerAngles.y >= 135 && transform.eulerAngles.y < 225){
  direction = 3;
  }else if(transform.eulerAngles.y >= 225 && transform.eulerAngles.y < 315){
  direction = 4;
  }else if(transform.eulerAngles.y >= 315 && transform.eulerAngles.y < 360){
  direction = 1;
  }    direction = 1;
 
 
 if(direction == 1){
 parent.renderer.material.mainTexture = tex1;
 }else if(direction == 2){
 parent.renderer.material.mainTexture = tex2;
 }else if(direction == 3){
 parent.renderer.material.mainTexture = tex3;
 }else if(direction == 4){
 parent.renderer.material.mainTexture = tex4;
 }
 
 }

and in the same object, I have this script,

 var daPlayer : GameObject;
 
 
 function Start(){
 daPlayer = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera");
 }
 
 function Update () {
 var lookPos = daPlayer.transform.position - transform.position;
 lookPos.y = 0;
 var rotation = Quaternion.LookRotation(lookPos);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 100);
 }


are the two scripts interfering with eachother? or is it another problem, because even with your suggestion, the direction stays at 1.

avatar image Phantomized · Feb 15, 2015 at 07:38 AM 1
Share

line 28 in the first script quote. You have an extra "direction = 1:" after the conditional statements. Try removing that.

avatar image eeveelution8 · Feb 15, 2015 at 07:37 PM 1
Share

didn't see that last one, it shouldn't have been there, thanks for pointing it out, because it works now.

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

20 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

Related Questions

Forward and back movements with a camera emulating an isometric view 1 Answer

Mouse axes based on position, not movement. 1 Answer

How Do i Prevent The First Person Camera From Turning 0 Answers

Camera rotates when I run into a wall 0 Answers

How to make the camera follow the player while still being able to be rotated? 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