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 cryptoshaman · Mar 06, 2013 at 12:45 AM · camerarotationparenting

Camera 360 not following parent

This Camera script zooms and orbits around Player with mouse. The problem is that when i rotate my player the camera stays still.

alt text

[CODE]

using UnityEngine; using System.Collections;

 [AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
 public class DragMouseOrbit : MonoBehaviour
 {
 public Transform target;
 public float distance = 5.0f;
 public float xSpeed = 120.0f;
 public float ySpeed = 120.0f;
  
 public float yMinLimit = -20f;
 public float yMaxLimit = 80f;
  
 public float distanceMin = .5f;
 public float distanceMax = 15f;
  
 public float smoothTime = 2f;
  
 float rotationYAxis = 0.0f;
 float rotationXAxis = 0.0f;
  
 float velocityX = 0.0f;
 float velocityY = 0.0f;
  
 // Use this for initialization
 void Start()
 {
 Vector3 angles = transform.eulerAngles;
 rotationYAxis = angles.y;
 rotationXAxis = angles.x;
  
 // Make the rigid body not change rotation
 if (rigidbody)
 {
 rigidbody.freezeRotation = true;
 }
 }
  
 void LateUpdate()
 {
 if (target)
 {
 if (Input.GetMouseButton(0))
 {
 velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
 velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
 }
  
 rotationYAxis += velocityX;
 rotationXAxis -= velocityY;
  
 rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
  
 Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
 Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
 Quaternion rotation = toRotation;
  
 distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
  
 RaycastHit hit;
 if (Physics.Linecast(target.position, transform.position, out hit))
 {
 distance -= hit.distance;
 }
 Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
 Vector3 position = rotation * negDistance + target.position;
  
 transform.rotation = rotation;
 transform.position = position;
  
 velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
 velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
 }
  
 }
  
 public static float ClampAngle(float angle, float min, float max)
 {
 if (angle < -360F)
 angle += 360F;
 if (angle > 360F)
 angle -= 360F;
 return Mathf.Clamp(angle, min, max);
 }
 }

[/CODE]

sphere rotation.jpg (98.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
Best Answer

Answer by prototype7 · Mar 06, 2013 at 03:02 AM

Try move the last bracket of Input.GetMouseButton(0)

 void LateUpdate()
     {
         if (target)
         {
             if (Input.GetMouseButton(0))
             {
                 velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
                 velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
             
 
             rotationYAxis += velocityX;
             rotationXAxis -= velocityY;
 
             rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
 
             Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
             Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
             Quaternion rotation = toRotation;
 
             distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
 
             RaycastHit hit;
             if (Physics.Linecast(target.position, transform.position, out hit))
             {
                 distance -= hit.distance;
             }
             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
             Vector3 position = rotation * negDistance + target.position;
 
             transform.rotation = rotation;
             transform.position = position;
 
             velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
             velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
             }
         }
 
     }

just make sure you attach the script in the Main Camera and drag your sphere to target in the inspector

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 cryptoshaman · Mar 06, 2013 at 04:52 AM

Hi proto

I implemented your change but did not get the result I was looking for here is an image to explain.

alt text


camera orbit problem.jpg (123.8 kB)
Comment
Add comment · Show 10 · 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 prototype7 · Mar 06, 2013 at 05:29 AM 1
Share

that's weird, send me your full code, including the code to make sphere rotate. Let me see if i can help to debug it.

avatar image cryptoshaman · Mar 06, 2013 at 06:37 AM 0
Share

Here is the full code after editing; The original is the ones posted above ;

public class Drag$$anonymous$$ouseOrbit : $$anonymous$$onoBehaviour { public Transform target; public float distance = 5.0f; public float xSpeed = 120.0f; public float ySpeed = 120.0f;

 public float y$$anonymous$$inLimit = -20f;
 public float y$$anonymous$$axLimit = 80f;
  
 public float distance$$anonymous$$in = .5f;
 public float distance$$anonymous$$ax = 15f;
  
 public float smoothTime = 2f;
  
 float rotationYAxis = 0.0f;
 float rotationXAxis = 0.0f;
  
 float velocityX = 0.0f;
 float velocityY = 0.0f;
  
 // Use this for initialization
 void Start()
 {
 Vector3 angles = transform.eulerAngles;
 rotationYAxis = angles.y;
 rotationXAxis = angles.x;
  
 // $$anonymous$$ake the rigid body not change rotation
 if (rigidbody)
 {
 rigidbody.freezeRotation = true;
 }
 }
  
 void LateUpdate()
 {
 if (target)
 {
 if (Input.Get$$anonymous$$ouseButton(0))
 {
 velocityX += xSpeed * Input.GetAxis("$$anonymous$$ouse X") * distance * 0.02f;
 velocityY += ySpeed * Input.GetAxis("$$anonymous$$ouse Y") * 0.02f;
 }
  
 rotationYAxis += velocityX;
 rotationXAxis -= velocityY;
  
 rotationXAxis = ClampAngle(rotationXAxis, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
  
 Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
 Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
 Quaternion rotation = toRotation;
  
 distance = $$anonymous$$athf.Clamp(distance - Input.GetAxis("$$anonymous$$ouse ScrollWheel") * 5, distance$$anonymous$$in, distance$$anonymous$$ax);
  
 RaycastHit hit;
 if (Physics.Linecast(target.position, transform.position, out hit))
 {
 distance -= hit.distance;
 }
 Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
 Vector3 position = rotation * negDistance + target.position;
  
 transform.rotation = target.rotation;
 transform.position = position;
         
  
 velocityX = $$anonymous$$athf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
 velocityY = $$anonymous$$athf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
 }
  
 }
  
 public static float ClampAngle(float angle, float $$anonymous$$, float max)
 {
 if (angle < -360F)
 angle += 360F;
 if (angle > 360F)
 angle -= 360F;
 return $$anonymous$$athf.Clamp(angle, $$anonymous$$, max);
 }
 }
avatar image prototype7 · Mar 06, 2013 at 07:42 AM 0
Share

i edited my answer

avatar image prototype7 · Mar 09, 2013 at 06:33 AM 1
Share

This is my final answer,

in the void Start() initialize your target rotation, in this case the sphere and think about axis, x rotation is y axis, and yrotation is x axis. And your Quaternion rotation is incorrect too.

i simplify your code according to your problem

 void Start()
     {
         if (settings.vertical_mirror) mirror_y = 1;  // vertical mirror
         if (!settings.showCursor) Screen.showCursor = false;   
         
         // initialize your target rotation, in this case the sphere
         Vector3 angles = transform.eulerAngles;
         // think about axis, x is y, and y is x
         rotationXAxis = angles.y;
         rotationYAxis = angles.x;
     }
 
     void LateUpdate()
     {
         if (Input.Get$$anonymous$$ouseButton(0))
         {
             if (target)
             {
                 // 
                 rotationXAxis = rotationXAxis + Input.GetAxis("$$anonymous$$ouse X") * 100 * 0.05f;
                 rotationYAxis = rotationYAxis + Input.GetAxis("$$anonymous$$ouse Y") * 100 * 0.05f * mirror_y;
 
                 Quaternion rotation = Quaternion.Euler(rotationYAxis, rotationXAxis, 0);
 
                 Vector3 position = rotation * new Vector3(0, 0, -10) + target.position;
                 transform.rotation = rotation;
                 transform.position = position;
             }
         }
     }
avatar image Kleptomaniac · Mar 09, 2013 at 07:08 AM 1
Share

$$anonymous$$ake sure that when you're done you give @prototype7 credit where credit is due by accept his answer!

Show more comments

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

12 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

Related Questions

1st person Camera rotates suddenly in another direction when unparenting from a platform 1 Answer

Parenting camera to object but keeping rotation? 1 Answer

Child affecting parent rigidbody? 1 Answer

Quaternion - Euler Angles, weird variable swapping when converting 2 Answers

i want the camera stop follwing my player only on y axis when he jumps... 4 Answers


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