• 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 Bit_Bendr · Sep 11, 2014 at 10:47 AM · orbittween

Tween code for RotateAround to a Vector3

Still pretty new to Unity and Quaternion math but I think I can explain this. I created the attached image to help.

alt text

I have this pillar located at 0,0,0 and a camera that orbits around at a fixed distance and is always looking at the center of the pillar. What I want is to be able click on tiles A or C and have the camera (no matter is current position) tween to points B or D respectively. But it must travel in an arc (the dashed line) so as to remain at that static distance. At the same time the camera must slerp so that its Y axis is pointing straight up while the Z axis looks at the tile's position.

Here's what I have so far but no idea how to "tween" along the arc. I hesitate to show this as it may be useless in light of a better solution.

 void Update(){
 Vector3 relativePos = _activePanel.transform.position - MainCamera.transform.position;
 Quaternion rotation = Quaternion.LookRotation(relativePos);
 
 Quaternion current = MainCamera.transform.localRotation;
 // this handles the look rotation pretty well
 MainCamera.transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime * 3f);
 // this section however should be my tween logic
 MainCamera.transform.Translate(3 * Time.deltaTime, 0, 0);
 }

Thanks!

rotatearoundto.jpg (18.3 kB)
Comment
Add comment · Show 3
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 Baste · Sep 11, 2014 at 10:56 AM 0
Share

It's kinda hard to tell from your drawing, so I'll ask: is the camera moving on the y-axis?

As far as I can tell, you want the camera to - Face the pillar - Be at the same height as the click - Be in a straight line out from the click

Is that correct?

avatar image VesuvianPrime · Sep 11, 2014 at 11:36 AM 0
Share

I'm after the same information as Baste. Can you clarify:

  1. Does the Camera move in the Y axis? (up and down the pillar)

  2. Does the Camera move in the Z axis? (towards and away from the pillar)

I'd also suggest taking a look at Coroutines. Separating your tween logic away from the Update method might help you to better visualize a solution.

avatar image Bit_Bendr · Sep 12, 2014 at 12:51 AM 0
Share

Thanks guys, sorry I didn't clarify that. You're both correct. The camera is intended to move up or down in Y as needed so as to point directly at the center of the tile when complete. (positioned at points B or D) I didn't want to confuse my question by talking about the Z axis as I'm pretty sure I'll be able to extrapolate that functionality once I've covered this problem. I will most likely use Coroutines in the end so if that ends up making it easier to explain to me feel free.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Baste · Sep 11, 2014 at 03:42 PM

Your solution seems way too complicated. It's often easier to do less maths and more Unity! I'm assuming that you want to move the camera on the y-axis as well as rotating it around the pillar, to be at height with the click.

If that's the case, solve your problem like this:

First, set the camera as a child of an empty object that's in the center of the pillar. The camera should be looking directly at this empty object.

Then, when the player clicks a point on the pillar, you do two things:

  • rotate the camera's parent to face the click. This will make the camera rotate, at a fixed distance from the pillar.

  • Move the camera's parent to the same y-coordinate as the click.

  • Both of these things needs to be done over time, with Vector3.MoveTowards and Vector3.RotateTowards. You probably want to time the rotation and y-movement to finish at the same time, so you get a smooth experience.

In this way you'll get exactly the movement you want, without using any difficult maths that'll take a bunch of headache.

Remember, whenever you want something in Unity to orbit another object, rotating parent objects is a ton easier than doing the maths. Also, whenever you do anything whatsoever with Quaternions, take a second to figure out if you can do it without Quaternions. That'll usually be the case, and easier.

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 Bit_Bendr · Sep 12, 2014 at 01:20 AM 0
Share

I will definitely consider this but I should mention that the camera is bound to a separate orbit system when a tile is not active. That is, I can click and drag to cause the camera to orbit anywhere around the pillar where its center focal point is clamped with the Y axis. That doesn't remove this parented solution but it will mean I have to back track and consider the wide spread impact. I$$anonymous$$O it would actually be simpler to devise a way to tween between any two points on a sphere's surface. It has to have been done before.

avatar image
0

Answer by Owen-Reynolds · Sep 11, 2014 at 02:45 PM

I think you're going about it the wrong way. Instead, imagine the square with the blue line at A, and having it move up and around to B. Then the camera is just along for the ride (it's glued to the end of the stick, so is always in the correct spot, looking the correct way. If fact, you could possibly just child the camera that way.)

If you simply lerp from A to B, you'll go inside the pillar, which would slightly reduce cam distance. Plus you can to compute the new angle each time, and might go through the center and snap-flip angles. Instead, I'd think of the movement in 2 parts: up/down and angle.

Say that A to B is currently 60 degrees and 4 meters up. Simple circumference math can tell you the distance for 60 degrees, if you like. But either way, do some rough math -- maybe it takes 1 second for each 60 degrees and 2 seconds for 4 meters, so 3 seconds total. I'm assuming you want movement to feel smooth.

Then lerp from 0 to 60 degs, and 0 to 4 meters, both over 3 seconds. At each point, you can compute the exact position and facing of the square from the new angle/ht, as well as the line coming out of it from the center. Place the camera there, looking at the square.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Orbit Sim without Physics Engine 1 Answer

Mouse/Wacom stylus drag not working. 1 Answer

Orbit Cam with different Views 0 Answers

Translating Speed into rotation degress\sec 1 Answer

How Do I Simulate First-Person Without Clipping Player Model? 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