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 Abacab · Feb 12, 2015 at 11:22 PM · c#cameracar

Camera turns to the front when reversing car

Hello!

When I started making my game back in 2014 I followed a video tutorial and created a camera script with the help of that. When reversing though, the camera turns to the front of the car and I don't like that. How would I get the camera to always look at the back of the car?

 using UnityEngine;
 using System.Collections;
 
 public class CarCameraScript : MonoBehaviour
 {
     
         public Transform car;
         public float distance = 6.4f;
         public float height = 1.4f;
         public float rotationDamping = 3.0f;
         public float heightDamping = 10.0f;
         public float zoomRatio = 0.5f;
         public float defaultFOV = 60f;
         private Vector3 rotationVector;
     
         void LateUpdate ()
         {
                 float wantedAngle = rotationVector.y;
                 float wantedHeight = car.position.y + height;
                 float myAngle = transform.eulerAngles.y;
                 float myHeight = transform.position.y;
         
                 myAngle = Mathf.LerpAngle (myAngle, wantedAngle, rotationDamping * Time.deltaTime);
                 myHeight = Mathf.Lerp (myHeight, wantedHeight, heightDamping * Time.deltaTime);
         
                 Quaternion currentRotation = Quaternion.Euler (0, myAngle, 0);
                 transform.position = car.position;
                 transform.position -= currentRotation * Vector3.forward * distance;
                 Vector3 temp = transform.position; //temporary variable so Unity doesn't complain
                 temp.y = myHeight;
                 transform.position = temp;
                 transform.LookAt (car);
         }
     
         void FixedUpdate ()
         {
                 Vector3 localVelocity = car.InverseTransformDirection (car.rigidbody.velocity);
                 if (localVelocity.z < -0.5f) {
                         Vector3 temp = rotationVector; //because temporary variables seem to be removed after a closing bracket "}" we can use the same variable name multiple times.
                         temp.y = car.eulerAngles.y + 180;
                         rotationVector = temp;
                 } else {
                         Vector3 temp = rotationVector;
                         temp.y = car.eulerAngles.y;
                         rotationVector = temp;
                 }
                 float acc = car.rigidbody.velocity.magnitude;
                 camera.fieldOfView = defaultFOV + acc * zoomRatio * Time.deltaTime;  //he removed * Time.deltaTime but it works better if you leave it like this.
         }
 
 }
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 InvincibleCat · Feb 12, 2015 at 11:31 PM

You can make things easy having 2 cameras and activate the one you need ;)

Comment
Add comment · Show 8 · 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 Abacab · Feb 12, 2015 at 11:40 PM 0
Share

Isn't there a way to make it work with only one? :(

avatar image InvincibleCat · Feb 12, 2015 at 11:42 PM 0
Share

Yes but why do you want just one ?

(by the way, can you please accept my answer here http://answers.unity3d.com/questions/898257/time-difference-between-race-time-and-best-time.html you just closed the post...)

You can have 2 tranforms representing your camera state and swith to this 2 transforms -> your camera become a child and then set the localPosition and the localRotation to 0 (of your camera)

avatar image Abacab · Feb 12, 2015 at 11:51 PM 0
Share

How do I activate and deactivate cameras in the script?

avatar image InvincibleCat · Feb 12, 2015 at 11:53 PM 0
Share

What I will do is having a new script that will have 2 gameobjects (the 2 cameras) and will activate/desactivate the cameras in your if/else statement (with SetActive method)

avatar image Abacab · Feb 12, 2015 at 11:54 PM 0
Share

To which object should I attach the script?

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

20 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

Related Questions

Distribute terrain in zones 3 Answers

Reverse Camera Twitches 1 Answer

Laggy Camera 2 Answers

Multiple Cars not working 1 Answer

Disable Camera Movement,Stop camera movement 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