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 SoshJam · Aug 17, 2017 at 11:38 PM · quaterniontransform.rotationroll a ball

Orbital Camera for Roll a Ball Game

OK, so I am trying to make a game sort of like the game in the Roll-A-Ball tutorial, but I need the camera to orbit. I have it rotating around the Y-axis just fine, so you can turn, but you can't look above you or anything like that. I can get it to go along the X-axis, like it's supposed to, with the addition of another Quaternion, but when you move the mouse diagonally, it rotates on the Z-axis too, so you're looking at the scene sideways. I really don't want this, is there a way I can fix it?

This is my script for a Camera Rotating function with only rotating on the Y-axis:

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     public class CameraRotate : MonoBehaviour
     {
     
         public Transform target;
         public float distance = 10.0f;
         public float sensitivity = 3.0f;
     
         private Vector3 offset;
     
         void Start()
         {
             offset = (transform.position - target.position).normalized * distance;
             transform.position = target.position + offset;
         }
     
         void Update()
         {
             Quaternion a = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * sensitivity, Vector3.up);
             offset = a * offset;
             transform.rotation = a * transform.rotation;
             transform.position = target.position + offset;
         }
     }


And that works fine. But this is what I have for making it rotate along the X-axis, but unintentionally going on the Z-axis too:

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     public class CameraRotate : MonoBehaviour
     {
     
         public Transform target;
         public float distance = 10.0f;
         public float sensitivity = 3.0f;
     
         private Vector3 offset;
     
         void Start()
         {
             offset = (transform.position - target.position).normalized * distance;
             transform.position = target.position + offset;
         }
     
         void Update()
         {
             Quaternion a = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * sensitivity, Vector3.up);
             Quaternion b = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * sensitivity, Vector3.right);
             Quaternion q = a * b;
             offset = q * offset;
             transform.rotation = q * transform.rotation;
             transform.position = target.position + offset;
         }
     }


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 nt314p · Aug 17, 2017 at 11:56 PM

Quaternions are tricky like that. Try using Transform.Rotate:

https://docs.unity3d.com/ScriptReference/Transform.Rotate.html

Hope this helps and good luck with your game!

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 SoshJam · Aug 18, 2017 at 01:10 AM 0
Share

I know how to use Transform.Rotate, but how would I use it in this case? How would I implement mouse movement?

avatar image
0

Answer by Eno-Khaon · Aug 18, 2017 at 08:05 AM

The rotations you're applying using that approach aren't using the coordinate system you think they are.

Specifically, by applying incremental modifications to a maintained Vector3-based offset, they need to be applied relative to the offset's relative position from the target. In other words:

 Quaternion b = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * sensitivity, Vector3.right);
 
 // Change this to...
 Quaternion b = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * sensitivity, transform.right);

With that, you'll have a quick fix, but it doesn't solve a much more fundamental problem when looking further ahead:

Your Y-axis needs to be limited so you can't flip the view upside down unintentionally.

It is quite disorienting when it's not the intended behavior...

Quaternions provide a single rotation to do what you want handled in two, which means it's much simpler to slightly modify your approach than it is to divide the rotation into multiple others.

To do that, rather than tweaking an existing offset, you can create a new rotation and treat that as your immediate baseline for a similar, yet more-easily-customizable effect.

To give an example of this in action:

 // C#
 public float yMin = -20;
 public float yMax = 40;
 float xRotation = 0;
 float yRotation = 0;
 public Transform target;
 public float distance = 10.0f;
 
 void Update()
 {
     Vector2 controlInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
     xRotation += Mathf.Repeat(controlInput.x, 360.0f);
     yRotation -= controlInput.y;
     yRotation = Mathf.Clamp(yRotation, yMin, yMax);

     Quaternion newRotation = Quaternion.AngleAxis(xRotation, Vector3.up);
     newRotation *= Quaternion.AngleAxis(yRotation, Vector3.right);
     transform.rotation = newRotation;
     transform.position = target.position - (transform.forward * distance);
 }

Okay, let's break this down now:

First, it's not actually much different than it was before. There are just a few key changes:

1 - I define a new, absolute rotation per frame rather than keeping one going. This lets me use the xRotation and yRotation values to strictly dictate the current, absolute rotations.

2 - I clamp the yRotation value between a minimum and maximum value to limit the angle to be suitable for gameplay.

3 - I set the position manually, based on the normalized rotation Quaternion and the distance variable to pull the camera as far away as intended.


To note, xRotation is sent through Mathf.Repeat() to avoid potential floating point accuracy issues at extremely high values (in the millions of degrees it would become more noticeable, but it's better off prevented entirely).

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

70 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

Related Questions

How to rotate the instantiated object automatically? 1 Answer

How to combine multiple rotations at once? 0 Answers

Adding Rotation to a Gameobject 1 Answer

Vertical Camera Orbit 0 Answers

Follow rotation on Y axis ONLY clockwise/counterclockwise. 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