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 EvanDela · Sep 23, 2011 at 07:21 AM · rotationeuler

Camera Script Rolling on the Z

Hi All, I wrote this amazing script (stole it) and have modified sections (ripped it appart) to make it work as I want it to (it isn't working)

All jokes aside, I am attempting to rotate the camera around the screen with it's focus on a target. The rotation happens on a mouse drag. The rotatin is full 360. The problem is that the camera swivles on the Z axes as well as the x and y.

Please can someone help me out here. I basically take the code below, and attatch it to the Main Camera.

using UnityEngine; using System.Collections;

public class CameraControl : MonoBehaviour {

 public Transform target;
 public float xSpeed = 125.0f;
 public float ySpeed = 50f;

 public float  resetTime = 1.0f; //How long to take to reset

 private Vector3 startDirection = Vector3.zero; //How far away to orbit

 private float x = 0.0f; 
 private float y = 0.0f;
 private float z = 0.0f; 

 private Quaternion startRotation;
 private Quaternion rotation;

 private bool resetting = false;
 private float resetTimer = 0.0f;

 void LateUpdate()
 {
     if (Input.GetMouseButtonDown(0))
     {    startDirection = (transform.position - target.position);
         startRotation = transform.rotation;
         x = 0.0f;
         y = 0.0f;
         z = 0.0f;
     } 

     if (Input.GetMouseButton(0))
     {
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
         z = 0.0f;
         Reorient();
     } 
 }

 void Reorient()
 {
     rotation = Quaternion.Euler(y, x, z);
     transform.rotation = rotation * startRotation;
     transform.position = rotation * startDirection + target.position;
 }

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tommynator · Sep 23, 2011 at 10:47 AM

Easy - Just modify your rotation Quaternion and let it rotate only around the y-axis, by taking only your horizontal mouse movement.

rotation = Quaternion.Euler(0, x, 0);

done :)

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 EvanDela · Sep 25, 2011 at 11:47 PM

That works really well but it restricts me to the x-axis, in this instance I want to be able to revolve around an object by using the left and right as well as use the mouse up and down to move the camera from a side view to a top down view, the problem is that your modification restricts me moving the camera up and down now :( any other suggestions ?

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 EvanDela · Sep 25, 2011 at 11:47 PM

Thats great, I posted a respose, but it doesn't seem to have appeared, so I am going to try again. Firstly - thanks very much for the answer. It dows prevent the z-axis rolling - the problem is that it now has restricted my y axis movement too. When I try this...

rotation = Quaternion.Euler(x, y, 0);

The damned thing still rolls on the Z. This is just making me crazy.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need help in setting a game-object's rotation to euler-degrees. 0 Answers

Is there no way to get reliable angles from a rigidbody? 2 Answers

Rotation problem: Check is failing/ endlessly spins 1 Answer

Boat's rotation in relation to the rudder 1 Answer

Spawn Rotation not Working? 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