Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 XskodeMedia · Jan 02, 2020 at 07:07 PM · rotationenemyplayer movementworldspacelocalrotation

how do i include enemy's rotation within my checking of if player is on the right side of enemy?

Okay (problem in code is at line 61 thru 86)
the code is basicly checking if player is within a certain distance if so, then enemy will turn (within a time frame) towards player.

The Problem: When enemy rotates towords player the game still reads that the player is still on the right side of enemy. however, as you can see in the photo, the player is actually standing right in front of the enemy.

My Question:
How can i make sure that when the enemy rotates that the game also reads the player's position correctly. meaning when the enemy rotates to face the player the game knows that the player is no longer standing on the right side of enemy but instead is now standing infront of enemy?

I hope this is understandable if not just ask your question(s) i will answer.

What I am trying to accomplish:

so the enemy is minding it's own business. however, when the player comes to the enemy then the enemy will react correctly based on which side the player is on.

[[ clarity ]]:

player sneaks up on left side; enemy jumps to right and runs. player sneaks up on right side; enemy turns towards player and attacks. etc...

The photo shows both the code and the problem issue. (in the editor within the picture the enemy has already rotated towards the player. but the game is still reading the player as if they are on the right side of enemy still). [you can right click on image and open in new tab to get a high resolution of the image and read everything clearly]
vars:
target = player transform
pposition = player position
eforward = enemy's forward direction

alt text

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

Answer by unity_ek98vnTRplGj8Q · Jan 02, 2020 at 09:44 PM

The problem is this line here

 if(target.position.x > transform.position.x && target.position.x < (transform.position.x + 10)){

You are checking if the GLOBAL position of the player is in the GLOBAL x direction, which does not at all take into account the rotation of the enemy unit. There are 100 different ways that you could do this, but we might as well use similar code to what you are already doing using the dot product. Notice that I am removing the y component of these vectors, just to make sure the calculation ignores any weirdness in the vertical axis.

 Vector3 rightFlat = transform.right;
 rightFlat.y = 0;
 
 Vector3 targetDirectionFlat = target.position - transform.position;
 targetDirectionFlat.y = 0;
 
 if(Vector3.dot(rightFlat, targetDirectionFlat) >= 0){
     //Player is to the right
 } else{
     //Player is to the left
 }

Comment
Add comment · Show 1 · 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 unity_ek98vnTRplGj8Q · Jan 02, 2020 at 09:45 PM 0
Share

Just a note, pposition is the player direction, not its position.

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

157 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 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 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 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 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 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 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

How can I rotate the player to fixed rotation? 1 Answer

How do you set a local rotation for 1 axis but keep the other axis world space untouched?,How do you set the local rotation of 1 axis without effecting the world rotation of others? 1 Answer

How can I pitch and roll a circular platform without releasing vertical/horizontal input? 1 Answer

Code to rotate around a local axis until local Y is 0? 0 Answers

Local child rotation and transform.LookAt 2 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