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 inspired997 · Mar 02, 2020 at 01:28 AM · gamecamera rotatefollowcylinderalong

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

I have a mini project where a ball goes around a cylinder while moving forward on the cylinder. I want my camera to follow the ball and rotate towards the ball while in cameras point of view cylinder is always forward. So for example if the ball goes to the right i want the camera to follow the ball to the right but point at the cylinder all the time and not rotate with ball to the right same problem like here: https://answers.unity.com/questions/1680243/camera-follow-ball-along-cylinder.html

I have a code but doesn't work the way I want.

 Transform target;
 public Transform pos;
 public float speed = 10f;
 void Start(){
     target = GameObject.FindGameObjectWithTag ("Player").transform;
 }
 void LateUpdate(){
 
     Vector3 rot = Quaternion.LookRotation (target.position - transform.position).eulerAngles;
     rot.z = 0;
     transform.rotation = Quaternion.Euler (rot);
 
 
 }
 void FixedUpdate(){
     transform.position = pos.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

1 Reply

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

Answer by Namey5 · Mar 02, 2020 at 12:20 PM

It's difficult to understand exactly what you are going for without further description/images etc. Also the answer would need to know how you are handling the ball movement, but here is a basic camera script that follows the ball around the cylinder assuming the cylinder is at the origin;

 public class CylinderCam : MonoBehaviour
 {
     //The ball transform
     public Transform target;
     //Camera height
     public float height = 2f;
     //Camera distance
     public float distance = 1f;
 
     private void LateUpdate ()
     {
         //The forward direction of the cylinder (replace this with your context)
         Vector3 cylinderDir = Vector3.forward;
         //This is the direction from the cylinder to the ball (which is just the ball's position, assuming the cylinder is at the origin
         Vector3 targetDir = target.position;
         //Here we use the vector projection property of the dot product to figure out the up direction of the cylinder relative to the ball
         Vector3 upDir = (targetDir - cylinderDir * Vector3.Dot (targetDir, cylinderDir)).normalized;
 
         //Move the camera to the required height and distance from the ball
         transform.position = target.position + upDir * height - cylinderDir * distance;
 
         //Here, we just figure out the look direction of the camera to the target, but instead we use our calculated up vector to rotate the camera around the cylinder
         Quaternion rot = Quaternion.LookRotation (target.position - transform.position, upDir);
         transform.rotation = rot;
     }
 }
Comment
Add comment · Show 4 · 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 inspired997 · Mar 02, 2020 at 08:55 PM 0
Share

This scirpt works when the ball rotates around the cylinder but doesnt go forward with the ball. And the cylinder is not at origin. I took the transform of cylinder and i wrote cylinderDir = cylinder_Transform.up;

avatar image inspired997 · Mar 02, 2020 at 11:07 PM 0
Share

I fixed it thank you very much for the script the way i fixed it was: Vector3 v = upDir height + cylinderDir distance; transform.position = target.position + v.normalized;

avatar image Namey5 inspired997 · Mar 02, 2020 at 11:30 PM 0
Share

Accidentally left out the target position, answer is updated.

avatar image inspired997 Namey5 · Mar 23, 2020 at 05:36 PM 0
Share

@Namey5 Now i have a car that rotates around a cylinder. When i press left mouse click the car rotates to the left and when i press right mouse click the car rotates to the right. What i want is when i release the buttons the car should rotate to point forward along the cylinder.

 rotation  = Input.GetAxisRaw("Horizontal");
     Vector3 yRot = Vector3.up * rotation * rotation_speed * Time.deltaTime;
     Quaternion deltaRot = Quaternion.Euler(yRot);
    Quaternion targetRot = rgb.rotation * deltaRot;
             
    rgb.$$anonymous$$oveRotation(Quaternion.Slerp(rgb.rotation, targetRot, 50f * Time.fixedDeltaTime));




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

158 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 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 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

Camera on circular path, following object movement 1 Answer

Player not following touch after camera is rotated? 0 Answers

How to make a cylinder follow two sphere 1 Answer

Camera rotation around player while following. 6 Answers

Texture following cursor... 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