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 AXXE78 · Aug 03, 2013 at 02:20 PM · camera rotatefollow

Camera on circular path, following object movement

Hi, I need to create this camera movement/behaviour:

![alt text][1]

A: the camera should follow the vertical movement of the object (the blue cube, which is moving freely around an arbitrary object in the center [the big cylinder]), thus always keeping it's Y position aligned with the Y position of the object

B: the camera should follow the horizontal movement of the object, but it should be constrained on a circular path

C: the camera should also be able to zoom in/out under certain circumstances

I tried different approaches but none of them is currently working. For example, I tried to modify the MouseOrbit script in order to recreate the circular movement of the camera; but while in the original script it's moving with the "Mouse X" input, I couldn't find a way to link the camera to the horizontal movement of the object (the blue cube):

 using UnityEngine;
 using System.Collections;
  
 public class MouseOrbitImproved : MonoBehaviour 
 {
     public Transform target;
     public float distance = 50.0f;
     public float xSpeed = 2.0f;
  
     float x = 0.0f;
    
     
     // Use this for initialization
     void Start () 
     {
         Vector3 angles = transform.eulerAngles;
         x = angles.y;
     }
  
     void LateUpdate () 
     {
         if (target) 
         {
             // Mouse X
             //x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
             
             // Move along with target
             // ???
             
             Quaternion rotation = Quaternion.Euler(0, x, 0);
 
             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
             Vector3 position = rotation * negDistance + target.position;
      
             transform.rotation = rotation;
             transform.position = position;
         }
     }
 }
question.jpg (122.2 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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by EHogger · Aug 03, 2013 at 03:55 PM

If the cylinder is always in the center, you can use that as a reference to find the position of the camera. (I'm sure there are other ways to do this, but I think this is simplest)

 using UnityEngine;
 using System.Collections;
 
 public class MouseOrbit : MonoBehaviour {
     
     public Transform target;
     public Transform cylinder;
     
     float cameraDistance = 50;
     
     void Start () {
     }
     
     void LateUpdate () {
         
         if (target)    {
             
             // First, lets match the vertical position:    
             transform.position = new Vector3(transform.position.x, target.position.y, transform.position.z);
             
             //    Now find the direction of the Target from the cylinder.
             //    We Want to ignore the vertical position of each:
             Vector3 targetXZ = target.position;
             Vector3 cylinderXZ = cylinder.position;
             targetXZ.y = 0;
             cylinderXZ.y = 0;
             
             // Now find the direction:
             Vector3 targetDirection = (targetXZ - cylinderXZ).normalized;
             
             //Now we have the direction, we can find the position for the camera relative to the Target:
             transform.position = target.position + (targetDirection * cameraDistance); //Editted this line to fix mistake.
             
             //Now we just have to rotate it to look at the target:
             transform.LookAt(target);
             
         }
     }
 }

You could then use Lerp or Slerp to smooth the movement if you wanted. I haven't tested this, so let me know if it works :)

Comment
Add comment · Show 3 · 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 AXXE78 · Aug 03, 2013 at 04:01 PM 0
Share

Wow, thank you very much. It's almost working. The only thing that is a bit off is the vertical position; it looks like the camera is not aligned with the Y position of the target object. Everything else is working as expected.

I had to change these lines:

 targetXZ.y = target.position.y;
 cylinderXZ.y = target.position.y;

because you were setting the Y to 0.

avatar image EHogger · Aug 03, 2013 at 04:02 PM 0
Share

Oh, and if you want to zoom in and out, just modify the cameraDistance variable

avatar image EHogger · Aug 03, 2013 at 04:07 PM 0
Share

Well spotted. Actually I meant to put target.position ins$$anonymous$$d of targetXZ on the final position setting.

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

15 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

Related Questions

Sync Camera and Object 1 Answer

Unity camera follow along cylinder looking for C# Expert!,Unity camera follow along cylinder Looking For C# Expert! 1 Answer

Player not following touch after camera is rotated? 0 Answers

Camera rotation around player while following. 6 Answers

Why is the Camera bouncing on the X-Axis?,Why does the Camera jump around on the X-Axis? 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