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
1
Question by SomeGuy22 · Jan 07, 2016 at 12:00 AM · rotationquaternioneuleranglesorbitrelative rotation

Mouse Orbit on a Different XY Plane?

This has been frustrating me for the past few hours...

I have this script that controls the camera:

 //Where x and y are Mouse controlled floats
 var rotation = Quaternion.Euler(y, x, 0);
 
 var position = rotation * Vector3(0, 0, -newDistance + 0.3) + target.position;
 
 transform.rotation = rotation;
 transform.position = position;

And that works fine for orbiting an object in world space. But I want the orbit to take place on the XY Plane associated with the Transform target. The target rotates as well, as I'm using something of a spherical world approach with it. But the camera always stays aligned to the world. I want the camera to rotate with the player but still retain the Y and X from that Euler, just relative to the player.

I've tried everything, including multiplying the Euler by the target rotation, casting the Euler as a direction and getting a LookRotation, and different combinations of the above... I think I need to step back from this. Thanks for the help, ideas are appreciated!

Comment
Add comment · Show 5
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 Fattie · Jan 07, 2016 at 12:07 AM 0
Share

I don't fully understand your question.

But it's very likely localPosition will help you.

$$anonymous$$ake your camera a child of one of your spheres (or whatever it is you have there) and then move the camera around only "within" that sphere or "marker object".

Hope it helps!

avatar image SomeGuy22 Fattie · Jan 07, 2016 at 12:58 AM 0
Share

The camera cannot be a child of the target. The camera has to be able to orbit around the object independent of the object's rotation. I just want the camera's y and x Euler to be relative to the target. Here's a picture of what I need:

alt text

targetexample.png (22.4 kB)
avatar image Fattie SomeGuy22 · Jan 07, 2016 at 03:30 PM 0
Share

So, note that I did not say the camera should be a child of the "target".

Rather it's extremely likely the camera has to be a target of "something" - perhaps the room, or perhaps a "marker" on the floor: the "marker" would follow the position of the target, but, simply always be set to the height of the floor.

I can assure you, you pretty much always do orbiting cameras in this way: rather than "calculating", just have an object (call it a marker object if you will) which the camera then orbits around in a simple way.

In some situations that object may be (for example) "the space ship" or "the room the player is in currently" or something like that .. or it may be a "marker" object as I describe above. The "marker" object in the example I gave would have a trivial three-line script, which keeps that marker object, as I say in the example "in the same position as the player but at floor level".

In the first instance you'd unit-test the marker object by using a bright red gizmo ball, and make sire that works. Only then, add the camera as an orbit from that.

I'm afraid I don't understand your scene description, so can't help more specifically.

You should certainly include some screen-shots of your game, a picture is worth 1000 words.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by SunnyChow · Jan 07, 2016 at 07:06 AM

Sorry that English is not my native language. I am still not sure what kind of camera movement you want. What I give you is a rotation completely relative to the target, Mmmm .... how about this one:

 rotation  =  Quaternion.FromToRotation(Vector3.up , target.up) * rotation ;
Comment
Add comment · Show 4 · 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 SomeGuy22 · Jan 08, 2016 at 01:30 AM 0
Share

Actually, SunnyChow's comment worked exactly for what I needed.

 rotation  =  Quaternion.FromToRotation(Vector3.up , target.up) * rotation;

It changes the Camera's up transform into the Player's up, which means the player will always face the correct way relative to the camera. Thanks!

avatar image Fattie SomeGuy22 · Jan 08, 2016 at 03:09 AM 1
Share

Great!

note for anyone else reading, it is common to do this exactly as I mention above, by making (some sort of) "marker" object that, for example, follows the player (or whatever) around on the floor. You then just orbit that.

(In particular, in this way you can quite easily add camera effects like lag, springiness etc.) Cheers!

avatar image SomeGuy22 Fattie · Jan 08, 2016 at 05:40 AM 0
Share

Hi, thanks for fixing the formatting! But for the sake of clarity, and the purpose of improving as a developer, I still don't understand how having the camera orbit a "marker" would solve my rotation issues. If the marker simply followed the position of the player, the player could still move upside down without the camera changing... $$anonymous$$aybe I'm missing something here, but could you clarify how it intends to fix the issue? (And sorry for being a nuisance!)

avatar image XxTutoredPowerxX · Sep 12, 2017 at 06:02 PM 0
Share

THAN$$anonymous$$ YOU SIR. I've been struggling with an orbit script for a character that can walk on walls for hours now, that simple line made it relative to it's current rotation. Thanks!

avatar image
1

Answer by SunnyChow · Jan 07, 2016 at 04:02 AM

  var rotation = Quaternion.Euler(y, x, 0);
  rotation  =   target.rotation * rotation ;
  
  var position =  rotation * Vector3(0, 0, -newDistance + 0.3) + target.position;
  
  transform.rotation = rotation;
  transform.position = position;

"target.rotation * rotation" or "rotation * target.rotation", I am not sure.

Comment
Add comment · Show 3 · 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 SomeGuy22 · Jan 07, 2016 at 05:05 AM 0
Share

No, Quaternion multiplication adds Left-hand rotations to Right-hand. The camera has to keep the orbit position, where the code above just makes the camera spin whenever the target rotates. Both orders of the equation failed. The only time Transform.rotation multiplication operation makes something local to that rotation is when is when it's used with a Vector3 Direction.

avatar image Fattie · Jan 07, 2016 at 03:31 PM 0
Share

All of these calculations are basically wrong :) But you honestly really wouldn't do that, you do it "with game objects", using the calculating power of Transform as your friend

avatar image SunnyChow Fattie · Jan 08, 2016 at 01:42 AM 1
Share

I guess you are talking about parenting the camera to target.

There is some drawback to do this. If i disable any parent GameObject of target, the camera is also disable. If the target is destroyed, the camera is destroyed too unless i write some script to unparent before the destruction. Also, I want the camera move with lerp, it's more tricky to do that when parented.

Beside, to have better performance, you should do calculation with $$anonymous$$ath ins$$anonymous$$d of GameObject

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

34 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

Related Questions

How to get Angle float of specific axis.(Turret clamping related). 2 Answers

When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers

Smooth rotation in 90° increments 0 Answers

[OAFAT]Why does transform.rotation.x = 45 not work 3 Answers

Modifying the Relative Orientations of Two Cubes 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