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 Ari-Bouaniche · Dec 29, 2013 at 07:53 PM · c#rotationcamera rotate

Moving the camera according to two parameters

Hey everyone,

I have been trying for hours now to come up with a satisfactory method for this, and I can't, so hopefully some charitable folk here can give it a whirl and help me out... Thank you!

Here's the situation: I have a (rigidbody) ball rolling in a (maze-like) box. The box's rotation angles in X and Z are movable (I did that with Input.GetAxis) so that the box can tip backward / forward and left / right depending on what arrow key is pressed.

My problem lies with the Camera. I would LOVE IT if my camera could rotate in the same way as the box but, at the same time, I need it to follow the ball (which needs to be at the center of the screen) and to be at the exact same distance from it at all times.

I've managed to make the camera rotate, but it's the part about keeping the ball at the center of the screen that is driving me completely nuts, when I'm sure it's relatively easy :'( I am under the impression that the camera should be able to rotate around the ball in a circle (as I want the distance between it and the ball to be constant) but I can't, for the life of me, figure out how to do that...

I'm relatively new to coding and I'm using C#. Here's what I was able to achieve so far:

     public GameObject ball;
     public GameObject box;
     private Vector3 offsetPosition;
     private Vector3 offsetRotation;
 
     void Start () {
         offsetPosition = transform.position - ball.transform.position;
         offsetRotation = transform.eulerAngles - box.transform.eulerAngles;
     }
 
     void LateUpdate () {
         transform.position = ball.transform.position + offsetPosition;
         Quaternion offset;
         offset = Quaternion.Euler (offsetRotation);
         transform.rotation = box.transform.rotation * offset;
     }

Thank you so very much for any help you might give me!

EDIT: My question seems to be unclear, so I have taken a few screenshots of my scene to illustrate my point and what I am trying to do. Here goes.

This is what I want: This is good

And this is what I have now, with the script I have copied above.

Note: The ball gameObject in the script above is actually not the ball itself but a child Empty which is located at the exact center of the ball, and which is constrained by a small script to no rotation:

 transform.eulerAngles = new Vector3 (0, 0, 0);

With the gameboard tilted to the left: This is not good, but what I have...

not_good.png (261.0 kB)
good.png (368.0 kB)
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 emc233 · Dec 29, 2013 at 07:57 PM

To look at the ball all you need is transform.lookAt(ball) this would be in camera code.

Comment
Add comment · Show 5 · 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 Ari-Bouaniche · Dec 29, 2013 at 08:22 PM 0
Share

I had already investigated Transform.LookAt(). $$anonymous$$y problem was that it actually controls both the rotation and position of my camera, and that I myself wanted to control the rotation, so that the camera plane is constantly parallel to the gameboard plane (which tilts in every direction, like I said in the question...)

Any other idea, that would not entail using LookAt(), but that would kind of achieve the same thing (well, not exactly, but you get what I mean...)?

avatar image emc233 · Dec 29, 2013 at 09:59 PM 0
Share

transform.lookAt() only controls rotation

avatar image Ari-Bouaniche · Dec 29, 2013 at 10:04 PM 0
Share

O$$anonymous$$, but rotation is exactly the parameter I DON'T need controlled, because I'm alrady controlling it according to the gameboard (my box) rotation. Therefore the question, I guess, is: how can I constrain the camera to rotate according to the box but in a perfect circle around the ball... ?

avatar image emc233 · Dec 29, 2013 at 11:12 PM 0
Share

I reread this over and over again. Either I am not understanding you or you are trying to do something that is impossible.

Here is my last stab at the problem:

$$anonymous$$ake the camera a child of the ball and fix it a set height over it

avatar image emc233 · Dec 30, 2013 at 04:41 AM 0
Share

this problem sounds pretty mathy but is now much more clear. It looks like you may have to solve a problem with vectors in 3d space and stuff to get the ball exactly in the center at all times.

In terms of unity code, I would still parent the camera to the ball object and then rotate the parent object based on a function that involves the normal of the board.

If that doesn't work, you can look at other games similar to this and observe the camera's behavior. The way that you want it seems like the ball isn't moving, but the walls are tilting and moving.

I am done with this question. Good luck in unity!

avatar image
0

Answer by Ari-Bouaniche · Dec 30, 2013 at 12:56 AM

I'll take the liberty of posting one last picture (only two per question are allowed) to show what I get with the board tilted to the right... Still not good: Still not good :(


not_good2.png (201.5 kB)
Comment
Add comment · Show 2 · 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 Barachiel · Dec 30, 2013 at 07:35 AM 0
Share

So, essentially, you don't want the player to see the rotation of this board happening, ins$$anonymous$$d you're using the rotation of the board to control the ball. Is this correct?

avatar image Ari-Bouaniche · Dec 30, 2013 at 07:45 AM 0
Share

Yes, @Barachiel, exactly!!

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

20 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Roll a ball camera problem? 1 Answer

Third person follow camera on a sphere 1 Answer

Multiple Cars not working 1 Answer

How to Change rotation while preserving local horizontal rotation 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