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 Flash1Lucky · Mar 13, 2020 at 09:59 AM · camerabugnoob

Help fixing a bug with camera script

Hey, I'm a Unity noobie working on my first project and I've run into an interesting problem with it. The game is just a simple motorcycle that you can drive around. It goes forward with input from the vertical axis (like W/S or Up/Down Arrow) and turns the bike with input from the vertical axis (A/D or Left/Right). I've been working on a script for the camera and this is what I have:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class CameraFollow : MonoBehaviour
     {
     
         public Transform target;
         public float dstFromTarget = 12f;
         public float smoothTime = 0.5f;
         public float camHeight = 3f;
         public float camAngle = 5f;
     
         private Vector3 rotationSmoothVelocity;
         Vector3 currentRotation;
     
         private void LateUpdate()
         {
             SmoothFollow();
         }
     
         void SmoothFollow()
         {
     
             Vector3 targetPos = target.transform.eulerAngles;
             Vector3 camPos = transform.eulerAngles;
     
             currentRotation = Vector3.SmoothDamp(currentRotation, targetPos, ref rotationSmoothVelocity, smoothTime);
        
             transform.eulerAngles = currentRotation + new Vector3(camAngle, 0f, 0f);
     
             transform.position = target.position + new Vector3 (0f, camHeight,0f) - (transform.forward * dstFromTarget);
     
         }
     
     }

The script works great almost all the time. It follows the bike like I want it to as I drive except for one problem. It freaks out when the rotation goes from 360 to 0 or vice versa in the Y-direction. I'm kind of stumped on how I might fix this problem because I don't know how to make 360 to 0 a smooth transition. If anyone has any solutions, I'd love to hear them. Thanks 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
1
Best Answer

Answer by unity_ek98vnTRplGj8Q · Mar 13, 2020 at 02:58 PM

Instead of smoothing your Euler angles I recommend smoothing the entire rotation. Quaternion.Slerp will rotate smoothly from one rotation to another without having to worry about angle overflow.

 Quaternion cameraTilt = Quaternion.Euler(camAngle, 0 ,0);
 transform.rotation = Quaternion.Slerp(transform.rotation, targetPos * cameraTilt, speed * Time.deltaTime);

Maybe something like this

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 Flash1Lucky · Mar 14, 2020 at 10:42 PM

Awesome, that worked! Thanks a lot for the help. I don't feel like I understand quaternions as much as I would like to but I guess I understand it enough for now because I was able to get it to work. Here's the latest code I used for archive purposes.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraFollow : MonoBehaviour
 {
 
     public Transform target;
     public float dstFromTarget = 12f;
     public float speed = 5f;
     public float camHeight = 3f;
     public float camAngle = 5f;
 
     private void LateUpdate()
     {
         SmoothFollow();
     }
 
     void SmoothFollow()
     {
         Quaternion targetPos = target.rotation;
         Quaternion cameraTilt = Quaternion.Euler(camAngle, 0, 0);
 
         transform.rotation = Quaternion.Slerp(transform.rotation, targetPos * cameraTilt, speed * Time.deltaTime);
 
         transform.position = target.position + new Vector3(0f, camHeight, 0f) - (transform.forward * dstFromTarget);
     }
 
 }

Thanks again for the help! @unity_ek98vnTRplGj8Q

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

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

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

Weird lighting bugs 1 Answer

rotate with alt doesnt work 15 Answers

[SOLVED] anyone had this before? "Editor Camera already has a render stack set... recursive rendering? UnityEditor.DockArea:OnGUI()" 2 Answers

Editor Camera "cuts off" objects/terrain 2 Answers

Depth Fade Problem on distance in VR 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