Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 beetrootpie · Aug 19, 2013 at 03:58 PM · 2d gameaitopdown2d rotation

2D Rotation for aiming problem

Hi, I'm having a bit of trouble wrapping my head around this at the minute... I'm trying to get an aiming function working for my AI in a 2D top-down game (I'm using orthello, if that matters), currently I have this code:

 float planeDif = target.transform.position.z - this.transform.position.z;
 Vector3 worldTargetPos = (target.position - transform.position);
 worldTargetPos.z = planeDif;
 worldTargetPos = Camera.main.ScreenToWorldPoint(worldTargetPos);
 direction = -worldTargetPos.normalized;//I did this because otherwise the up/down aiming goes in the opposite direction (eg: char moves up, AI aims down)
 lookRotation = Quaternion.LookRotation(direction);
 transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);

This seems to track my character properly if I move up and down, but moving left and right messes everything up, if I move left it aims way down and if I move right it aims way up. I've tried playing around with rotation values and such but I can't get it to work and now im sort of stuck in a rut...any help would be appreciated.

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 robertbu · Aug 19, 2013 at 04:10 PM

You are doing a ScreenToWorldPoint() translation of a point that is already in world space. If I understand your code correctly, you are trying to get the object this script is a component of to point at the target in a top down game. If so, then you should be doing something like:

 Vector3 direction = (target.position - transform.position);
 direction.y = 0.0f;
 Quaternion lookRotation = Quaternion.LookRotation(direction);
 transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);

The 'direction.y = 0.0' makes sure the rotation stays 2D. If everything is a on a plane at the same 'y' value, it is not necessary.

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 beetrootpie · Aug 19, 2013 at 04:42 PM 0
Share

This works perfectly, I had something like this before but it wasn't working properly, I just looked again and it was just because the base rotation of the game object was off, I only had to correct it by 90 degrees and it works great, thanks for your help !

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

15 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

Related Questions

How to Rotate Editor Camera in 2D? 1 Answer

Look for the player isn't working 1 Answer

Help with 2D topdown combat 1 Answer

How can I clamp direction of RaycastHit2D? 0 Answers

Enemies spawning close/ on top of the Player. 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