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
1
Question by Manmax75 · Oct 06, 2012 at 12:29 PM · c#camerarotationobjectrelative

Rotate object ON THE CAMERA UP AXIS

Hey.

I've created a rotation script in C# that will rotate a barrel according to the mouse.

I want the object to spin ON THE CAMERA UP AXIS.

Not the world's up axis.

Not the barrel's up axis. (It could be laying in any orientation.)

Not the barrel's parent's axis.

I want to rotate on the CAMERA UP AXIS.

Thanks!

alt text

Thanks :)

untitled.png (187.3 kB)
Comment
Add comment · Show 7
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 Manmax75 · Oct 06, 2012 at 12:43 PM 0
Share

I tried that, but I wasn't entirely sure how to use it correctly.

EDIT: Code I tried.. Didn't quite work to what I wanted. It just moves left/right, up/down.

heldObject.transform.RotateAround(Vector3.zero, Vector3.up, -Input.GetAxis("$$anonymous$$ouse X") Time.deltaTime); heldObject.transform.RotateAround(Vector3.zero, Vector3.forward, -Input.GetAxis("$$anonymous$$ouse Y") Time.deltaTime);

avatar image rhys_vdw · Oct 06, 2012 at 01:27 PM 0
Share

@Fattie

Yes that will work. It just seems like overkill when you can just set the rotation directly.

avatar image Manmax75 · Oct 06, 2012 at 01:35 PM 0
Share

By left and right, I mean in relation to the picture I have given.

avatar image rhys_vdw · Oct 07, 2012 at 11:17 AM 0
Share

I'm confused, have you not run my code? It clearly works in world space. I've triple checked it to be sure.

The x and y position of the mouse is normalized and turned into a rotation around two axes. When you assign to rotation the rotation is set in world space, regardless of the previous rotation.

Also any rotation is achievable by rotating on two axes.

However you're right that he did want it in camera space. For that, RotateAround is required.

I will submit another answer then, since the question has been modified. (I assume by you). By the way, before you deleted your answer he specified that he wanted to barrel to spin on the camera's Z axis with the vertical movement, and the barrel's Y axis with the mouse's horizontal movement. Both our answers were wrong.

avatar image rhys_vdw · Oct 07, 2012 at 11:23 AM 0
Share

Also the question originally asked how to rotate the barrel on two axes.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Oct 06, 2012 at 11:04 PM

You could simply use Rotate - but specifying Space.World, so that the object would rotate around world axes:

 function Update(){
   transform.Rotate(0, Input.GetAxis("Mouse X") * speed, 0, Space.World);
 }

where speed is roughly the turning speed in degrees per second - I said roughly because the value returned by axis "Mouse X" is a small value proportional to how much the mouse moved since last frame, which depends on mouse sensitivity.

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 rhys_vdw · Oct 07, 2012 at 11:33 AM

 public class MouseRotate : MonoBehaviour {
     [SerializeField] float m_rotateSpeed = 2f;
 
     void Update() {
         transform.RotateAround(Camera.main.transform.up,
                 Input.GetAxis("Mouse X") * m_rotateSpeed);
     }
 }
 
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

13 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

Related Questions

Flip over an object (smooth transition) 3 Answers

RTS Camera Rotation and Movement 0 Answers

I need help with TPS controls! 0 Answers

Is there a way to lock my camera's rotation and movement on certain axis? 2 Answers

How to relative movement system based on camera direction 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