Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Questofiranon · May 22, 2016 at 06:15 AM · quaternioncamera rotatefirst-personquaternion.lookrotationfirstperson

First Person Auto Aim Rotation

Hi, I'm working on a first person game and I'm trying to implement a lock on feature similar to the lock on feature in Metroid Prime. Right now I'm using a basic character controller with a camera attached to it, so my basic aiming is done by rotating the character controller on the y axis and the camera on the x axis.

However, I've ran into some issues when I try to do a lock on. For the character's movement/ x rotation, I do this:

 Vector3 charLookPosition  = lockOnTarget.transform.position - transform.position;
 charLookPosition.y = 0;
 Quaternion charRotation = Quaternion.LookRotation(charLookPosition);
 transform.rotation = originalCharacterRotation * charRotation;


Only using this, the character successfully rotates around the character if I move left/right and the target is always centered horizontally on the screen. However, for the Camera I attempted the same thing, except setting the x to 0:

 Vector3 camLookPosition = lockOnTarget.transform.position - viewCamera.transform.position;
 camLookPosition.x = 0;
 Quaternion camRotation = Quaternion.LookRotation(camLookPosition);
 viewCamera.transform.localRotation = originalCameraRotation * camRotation;

With this in place the character object still does what I want when moving, but the camera only centers on the target if I keep the character and object lined up on the world's z axis. Otherwise, the camera rotates up and down as the character object orbits around the target.

I've tried using Quaternion.LookAt() and a few other solutions using Euler angles, etc. and I always end up with this specific issue. I'm still wrapping my head around quaternions and that might be why I don't see what I'm doing wrong here.

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 Questofiranon · May 23, 2016 at 05:42 AM

I got the behavior I wanted based on this solution http://answers.unity3d.com/comments/563502/view.html by @robertbu

This is what my code looks like now:

 //perform calculation for character rotation
 float distanceToPlane = Vector3.Dot(transform.up, lockOnTarget.transform.position - transform.position);
 Vector3 planePoint = lockOnTarget.transform.position - transform.up * distanceToPlane;
     
 Quaternion qCharacter = Quaternion.LookRotation(planePoint - transform.position, transform.up);
                     transform.rotation = originalCharacterRotation * qCharacter;
     
 //perform calculation for camera:
 distanceToPlane = Vector3.Dot(transform.up, lockOnTarget.transform.position - viewCamera.transform.position);
 planePoint = lockOnTarget.transform.position - viewCamera.transform.up * distanceToPlane;
                     
 Vector3 v3 = new Vector3(0.0f, distanceToPlane, (planePoint - viewCamera.transform.position).magnitude);
 Quaternion qCamera = Quaternion.LookRotation(v3);
     
 viewCamera.transform.localRotation = originalCameraRotation * qCamera;

It seems to work as I wanted, I had to do a second calculation because my camera has a bit of an offset from the character.

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

53 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

Related Questions

How do I do my camera player look at an specific direction when key pressed 0 Answers

How to rotate hands with camera in a FPS/TPS game when the character model and animation are complete? 0 Answers

Uniform quaternion from a list of points? 0 Answers

How do I orient one axis of an object following LookAt/LookRotation? 0 Answers

Camera rotation in maze game depending of accelerometer 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