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
0
Question by OldLadyNevermore · Jul 28, 2013 at 05:18 AM · c#cameraorbit

Orbit Camera around object

I have this script (from the script reference) that makes the camera orbit my selected object when I use the joystick. However, it only moves for a few degrees, and 'snaps' back when I let go of the joystick.

All these Quaternions, Eulers, and Slerps still confound me. Can someone see what I need to do with this to make the camera keep orbiting?

using UnityEngine; using System.Collections;

public class JoystickOrbit: MonoBehaviour { public Transform target; // This is the controller we are following public float smooth = 2.0F; public float tiltAngle = 30.0F;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 }
 
 void LateUpdate() { //Every frame, do this as late as you can
     float tiltAroundZ = Input.GetAxis("RightStickHorizontal") * tiltAngle;
      float tiltAroundX = Input.GetAxis("RightStickVertical") * tiltAngle;
     Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
     transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
 }

}

Comment
Add comment · Show 6
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 OldLadyNevermore · Jul 28, 2013 at 04:39 PM 0
Share

The code entered was giving errors about missing semi-colons on lines 14, 15, and 19; I don't know why the semi-colons are there.

Anyway, since I don't know javascript, but I know a bit of C#, I converted the code to that, and got it working, but it does the same thing: camera rotates a few degrees only then snaps back.

Does anyone have any further ideas?

using UnityEngine; using System.Collections;

public class JoystickOrbit : $$anonymous$$onoBehaviour {

 const float y$$anonymous$$inLimit = -360;
 const float y$$anonymous$$axLimit = 360;
 const float x$$anonymous$$inLimit = -360;
 const float x$$anonymous$$axLimit = 360;
 const float tiltAngle= 30.0f;
 const float smooth = 2.0f;
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }

 void LateUpdate() { //Every frame, do this as late as you can
     float tiltAroundY = Input.GetAxis("RightStickVertical") * tiltAngle;
     float tiltAroundX = Input.GetAxis("RightStickHorizontal") * tiltAngle;
     tiltAroundY = ClampAngle(tiltAroundY, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
     tiltAroundX = ClampAngle(tiltAroundX, x$$anonymous$$inLimit, x$$anonymous$$axLimit);
  
     Quaternion target = Quaternion.Euler(tiltAroundY, tiltAroundX, 0);
     transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
 }
  
 private int ClampAngle (float angle, float $$anonymous$$, float max) {
     if (angle < -360){
         angle += 360;
     }
     if (angle > 360){
         angle -= 360;
     }
     return $$anonymous$$athf.Clamp ((int)(angle), (int)($$anonymous$$), (int)(max));
 } 

}

avatar image AlucardJay · Jul 28, 2013 at 07:26 PM 0
Share

It could possibly be a problem with $$anonymous$$onoDevelop. When you copy and paste code into it, sometimes there are 'invisible' characters in the code. Sometimes this can be fixed by removing indentations(select all, shift+tab), then highlighting and deleting the blank space after every line. (then indent everything again). Sometimes you have to create a new line and type it in again, then delete the line with the error completely.

avatar image nixcs2512 · Jul 28, 2013 at 09:09 PM 0
Share

@OldLadyNevermore: i think it because the special thing about Quaternion ( or rotation ). The line Quaternion target = Quaternion.Euler(tiltAroundY, tiltAroundX, 0); means it will create a new Quaternion with the same value as previous LateUpdate (if you continue hold the axis) or 0 if you release the axis.The code may change a bit into this: Quaternion multiply= Quaternion.Euler(tiltAroundY, tiltAroundX, 0); Quaternion target = transform.rotation*multiply; if you want to return the rotation to the normal, just make a variable that holds the normal rotation at the beginning (or just before rotate)

avatar image nixcs2512 · Jul 28, 2013 at 09:11 PM 0
Share

Sorry for that unclear comment but i think the Code Sample button not work for me. Hope you can get it.

avatar image OldLadyNevermore · Jul 28, 2013 at 09:58 PM 0
Share

I tried changing that section of code to read like this; but the issue persists:


        Quaternion multiply= Quaternion.Euler(tiltAroundY, tiltAroundX, 0); 
        Quaternion target = transform.rotation*multiply;
        //Quaternion target = Quaternion.Euler(tiltAroundY, tiltAroundX, 0);
        transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by daivd.ramz · Jul 28, 2013 at 07:07 AM

hi, i don't work with joystick still but con you try this:

 var yMinLimit:float = -360;
 var yMaxLimit:float = 360;
 var xMinLimit:float = -360;
 var xMaxLimit:float = 360;
 
 void LateUpdate() { //Every frame, do this as late as you can

 float tiltAroundY = Input.GetAxis("RightStickHorizontal") * tiltAngle;
 float tiltAroundX = Input.GetAxis("RightStickVertical") * tiltAngle;
 tiltAroundY = ClampAngle(tiltAroundY, yMinLimit, yMaxLimit);
 tiltAroundX = ClampAngle(tiltAroundX, xMinLimit, xMaxLimit);

 Quaternion target = Quaternion.Euler(tiltAroundY, tiltAroundX, 0);
 transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
 }
 
 static function ClampAngle (angle : float, min : float, max : float) {
 if (angle < -360){
 angle += 360;
 }
 if (angle > 360){
 angle -= 360;
 }
 return Mathf.Clamp (angle, min, max);
 } 

i hope this work for you :)

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 OldLadyNevermore · Jul 30, 2013 at 01:43 AM

Okay, with everyone's help, I was able to reverse-engineer the mouseorbit script and get it working with joysticks.

Here is the final result (turn on zoom to disable up-and-down in favor of zoom):

using UnityEngine; using System.Collections;

public class JoystickOrbit : MonoBehaviour { public Transform target; public float distance = 10.0f; public float xSpeed = 250.0f; public float ySpeed = 120.0f; public float yMinLimit = -20f; public float yMaxLimit = 80f; public float zoomSpeed = 120.0f; private float x = 0.0f; private float y = .0f; public bool zoom;

 void Start () {
     Vector3 angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;

     // Make the rigid body not change rotation
        if (rigidbody) {
         rigidbody.freezeRotation = true;
         }
     }
 
 void LateUpdate () {
 if (target) {
     x += (float)(Input.GetAxis("RightStickHorizontal") * xSpeed * 0.02);
         
     if (zoom) {
         distance -= (float)(Input.GetAxis("RightStickVertical"));
     }
     else {
         y += (float)(Input.GetAxis("RightStickVertical") * zoomSpeed * 0.02);
     }
      
      y = ClampAngle(y, yMinLimit, yMaxLimit);
             
     Quaternion rotation = Quaternion.Euler(y, x, 0);
     Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
     
     transform.rotation = rotation;
     transform.position = position;
     }
 }

 private int ClampAngle (float angle, float min, float max) {
     if (angle < -360){
         angle += 360;
     }
         if (angle > 360){
         angle -= 360;
     }
     return Mathf.Clamp ((int)(angle), (int)(min), (int)(max));
 }

}

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

18 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

Related Questions

Enable the OrbitCam script how can I make it will continue from the current camera view and position ? 0 Answers

Multiple Cars not working 1 Answer

How can I make the camera to orbit also up and down ? 1 Answer

Distribute terrain in zones 3 Answers

Cinemachine FreeLook - Explicitly setting orbit 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