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 /
  • Help Room /
avatar image
0
Question by Castermaluney · May 17, 2016 at 04:50 PM · cameracamera-movementcamera follow

Camera follow issue.

So, I have a camera that has to follow the player that is a rolling ball, and i have the following script:

     public GameObject target;
     public Transform targetTransform;
     public Rigidbody targetRigidbody;
     
     private Transform camTransform;

     private float distance = 20.0f;
     private float height = 5.0f;
     private float rotationDamping = 0.2f;
     private float heightDamping = 10;
 
     void Awake()
     {
         camTransform = this.gameObject.GetComponent<Transform>();
 
         target = GameObject.FindWithTag("Player");
         targetTransform = target.GetComponent<Transform>();
         targetRigidbody = target.GetComponent<Rigidbody>();
     }

     void update(){
     //set the starting values

     float targetRotation;
     float targetHeight = targetTransform.position.y + height;
     float camRotation = camTransform.eulerAngles.y;
     float camHeight = camTransform.position.y;

     Vector3 flatSpeed= targetRigidbody.velocity;
     flatSpeed.y = 0;                         //flat speed of the player

     if (move == Vector3.zero)
         targetRotation = camRotation;
     else                                    //record Y rotation of flat speed if it is moving           
         targetRotation = Quaternion.LookRotation(move).eulerAngles.y;

     camHeight = Mathf.Lerp(camHeight, targetHeight, heightDamping * Time.deltaTime);
     camRotation = Mathf.LerpAngle(camRotation, targetRotation, rotationDamping * Time.deltaTime);


     Quaternion currentRotation;    //get the wanted rotation obtained through lerping
     currentRotation = Quaternion.Euler(0, camRotation, 0);

     Vector3 wantedPos = targetTransform.position - (currentRotation * Vector3.forward * distance);
     wantedPos.y = camHeight;
     camTransform.position = wantedPos;
     camTransform.LookAt(targetTransform);
                               //set position and rotation of the camera transform
 }


now, this scripts works preatty fine, it follows smoothly the ball from the back while it moves. my only problem is that, when i make the ball roll backward instead of forward the camera rotates 180 degrees and follows the player from the front insted, as you would expect. this gets the game quite confusing, and I really don't know how to fix this script. Or if anyone knows of a better script and could be so kind to post it here. Thank you in advance.

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 Castermaluney · May 18, 2016 at 01:48 PM

Ok I finally found out a solution, but I warn you thins only works on a rolling ball behaving like a wheel, like mine (it moves spinning only on its x axis); It's quite convoluted so i'll try to explain: since my ball spins on the on the x axis, I thought that rotating it by 90 degrees would turn it roughly into the "front" of the ball. so i just set as that the rotation of the camera around the ball.

Here's the code:

 private Transform playerTransform;
 private Transform camTransform;
 
 private float distance = 20.0f;
 private float height = 5.0f;
 private float rotationDamping = 0.2f;
 private float heightDamping = 10;
 
 void Awake()
 {
   camtransform = gameObject.GetComponent<Transform>();
   playerTransform = gameObject.FindWithTag("Player").GetComponent<Transform>();
 }
 
 void Update()
 {
   float currentAngle = camTransform.eulerAngles.y;
   float wantedHeight = targetTransform.position.y + height;
 
   Vector3 frontDirection = Quaternion.Euler(0, -90, 0) * targetTransform.right;
   frontDirection.y = 0; //flat vector just to be sure
 
   float wantedAngle = Quaternion.LookRotation(frontDirection).eulerAngles.y;
 
   //interpolation to have a damping effect
   float targetAngle = Mathf.LerpAngle(currentAngle, wantedAngle, rotationDamping * Time.fixedDeltaTime);
   float targetHeight = Mathf.Lerp(camTransform.position.y, wantedHeight, heightDamping * Time.fixedDeltaTime);
 
   //set the camera transform where it should be
   Quaternion currentRotation = Quaternion.Euler(0, targetAngle, 0);
   Vector3 wantedPos = targetTransform.position - (currentRotation * Vector3.forward * distance);
   wantedPos.y = targetHeight;
   camTransform.position = wantedPos;
   camTransform.LookAt(targetTransform);
 }


Hope it helps somebody.

Comment
Add comment · Show 1 · 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 Castermaluney · May 18, 2016 at 02:49 PM 0
Share

made an even better system: it still follows the speed but it inverts it when you are moving bakcward; haven't found a better system to check if you are moving backward.

 private Transform playerTransform;
 private Transform camTransform;
 private Rigidbody playerRigidbody;
  
 private float distance = 20.0f;
 private float height = 5.0f;
 private float rotationDamping = 0.2f;
 private float heightDamping = 10;
  
 void Awake()
 {
    camtransform = gameObject.GetComponent<Transform>();
    playerTransform = gameObject.FindWithTag("Player").GetComponent<Transform>();
    playerRigidbody = playerTransform.gameObject.GetComponent<Rigidbody>();
  }
  
 void Update()
 {
    float currentAngle = camTransform.eulerAngles.y;
    float wantedHeight = targetTransform.position.y + height;
    float wantedAngle;
  
    Vector3 frontDirection = Quaternion.Euler(0, -90, 0) * targetTransform.right;
    frontDirection.y = 0; //flat vector just to be sure
 
    Vector3 flatSpeed = targetRigidbody.velocity;
    flatSpeed.y = 0;
 
    float angleCheck = Vector3.Angle(frontDirection, flatSpeed);
 
    if (angleCheck < 90 && flatSpeed != Vector3.zero)
       wantedAngle = Quaternion.LookRotation(flatSpeed).eulerAngles.y;
    else if (flatSpeed != Vector3.zero)
       wantedAngle = Quaternion.LookRotation(-flatSpeed).eulerAngles.y;
    else    //if the ball isn't moving
       wantedAngle = Quaternion.LookRotation(frontDirection).eulerAngles.y;
       
  
    //interpolation to have a damping effect
    float targetAngle = $$anonymous$$athf.LerpAngle(currentAngle, wantedAngle, rotationDamping
 Time.fixedDeltaTime);
    float targetHeight = $$anonymous$$athf.Lerp(camTransform.position.y, wantedHeight, heightDamping * Time.fixedDeltaTime);
  
    //set the camera transform where it should be
    Quaternion currentRotation = Quaternion.Euler(0, targetAngle, 0);
    Vector3 wantedPos = targetTransform.position - (currentRotation * Vector3.forward * distance);
    wantedPos.y = targetHeight;
    camTransform.position = wantedPos;
    camTransform.LookAt(targetTransform);
 }

it still works only with a wheel like movement.

avatar image
0

Answer by Zoogyburger · May 17, 2016 at 09:08 PM

No exactly sure how you want the camera to follow the ball, but here is some very simple code:

 public Transform target;
 
     void Update()
     {
         if (target)
         {
             transform.position = Vector3.Lerp (transform.position, target.position, 0.1f)+ new Vector3 (0, 0, -10);
         }
     }
 }

Don't forget to set your ball as the target in the inspector!

Comment
Add comment · Show 1 · 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 Castermaluney · May 18, 2016 at 10:51 AM 0
Share

the problem with this script is that the camera doesn't rotate around as the ball turn's left or right, so it doesn't fix my problem.

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

64 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

Related Questions

Spyro Like Camera Follow 0 Answers

When attaching my custom camera script camera shakes when player starts to move fast. 1 Answer

C# Smooth Follow Space Ship 1 Answer

How to stop the camera when the player has reached the edge of the level? 2 Answers

Why is my camera shaking when I use my chaser script on something? 0 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