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 /
avatar image
0
Question by KravenArk · Dec 04, 2017 at 04:38 AM · rotationrotate object

Rotate an object based on Camera's axis

Hi So I have an assignment for class and I am having some trouble with this one part.

Basically I have an object that needs to rotate based on the view that the main camera sees it.

For example, The object is at 0,0,0. and such it's transform.forward is 0,0,1. But the camera's transform.forward is 0.5,0,0. I need the object to act as if it's transform.forward is 0.5,0,0.

Another example is that the object needs to jump in the direction of the camera. If the camera is sideways in the world, the player needs to jump sideways as well.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityVeris · Dec 04, 2017 at 05:43 AM

@KravenArk

You say the object needs to rotate based on the cameras axis', but you've described a translation (jumping).

I'm going to assume you mean translation:

 // Get the position of the GameObject you want to move
 // I'm assuming it's a player (playerObject)
 Vector3 newPos = playerObject.transform.position;
 
 // Modify the new position however you like
 // In this case we are modifying it with respect to the
 // camera (cameraObject) rotation
 newPos += (cameraObject.transform.right * (float)speed);
 
 // Set the player in the new location
 playerObject.transform.position = newPos;

You can use the following to get different directions:

transform.up = objects local +Y axis.

-transform.up = objects local -Y axis.

transform.right = objects local +X axis.

-transform.right = objects local -X axis.

transform.forward = objects local +Z axis.

-transform.forward = objects local -Z axis.

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 KravenArk · Dec 06, 2017 at 06:47 AM

That actually helped a lot. What I ended up doing was recording the forward direction of the camera and using that to direct the player when they rotate, move or jump. Essentially the idea is that the camera moves around the player but wherever the camera's forward is, the player will move that way. So the player doesnt have to adjust their motion based on the camera's location

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 UnityVeris · Jan 22, 2018 at 02:13 AM 0
Share

Glad that helped, sorry I wasn't entirely sure how to interpret parts of the question.

There are a bunch of other ways to go about it, like having an invisible dummy object that you move around to figure out new values, or parenting objects and then adjusting local coordinates, but I find just getting and setting like that is easy to think about, reads well and suits most situations.

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

98 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

Related Questions

Rotate object based on another rotation above a certain threshhold 1 Answer

gameobject stops moving correctly when rotating 1 Answer

Smooth rotation about global axis instead of local axis. 1 Answer

Rotating enemy object to face the player when moving 2 Answers

I need Some Info About Rotation And Pvot 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