Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 adamjamesnapier · Feb 15 at 07:03 PM · newbieissues

How to prevent Camera from Translating when rotating player

I have a camera script that follows my player, but whenever the player rotates the player's transform.position values also change. This is causing my camera to translate slightly while the player rotates. What I want is my camera to follow my player without taking rotation into the equation. I have tried setting my rotation to 0 and then grabbing the player.transform.position values, but that does not work either.

Attached is the Camera Follow Script. Any help would be greatly appreciated

alt text

alt text

sceneview.gif (419.9 kB)
camfollow.png (113.0 kB)
Comment
Add comment · Show 7
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 andrew-lukasik · Feb 15 at 09:17 AM 0
Share

This issue has nothing to do with the code. It is caused by target transform being rotated, that's all.

If you want to fix this place your target transform at the center of player rotation i.e. at center of the bike and not below it.

avatar image adamjamesnapier · Feb 15 at 01:30 PM 0
Share

Hey thanks for the quick reply! How exactly do I place my target transform at the center of player and not under it? You mentioned it has nothing to do with the code, but the code is just following the player transform plus an offset. I have removed the offset and the camera is still below the player. How do I make the transform of the player be at the center? It appears my Transform.x value is actually below my car and not in the center. I also tried moving my camera to be at the center of the player in the scene view, but the camera still moves whenever my player rotates. If you need any additional info I will be glad to share!

avatar image andrew-lukasik adamjamesnapier · Feb 15 at 01:50 PM 0
Share

You mentioned it has nothing to do with the code, but the code is just following the player transform plus an offset. I have removed the offset and the camera is still below the player

It is caused by the code - yes... but not directly. I meant there that code works fine and you shouldn't try to fix this issue changing the code (for worse) because it's caused by the target transform local position being in the wrong spot.

Add these lines to your script, to see what is going on better:

 #if UNITY_EDITOR
 void OnDrawGizmos ()
 {
     if( target!=null )
     {
         Gizmos.color = Color.yellow;
         Gizmos.DrawLine( transform.position , target.position );
         Gizmos.DrawSphere( target.position , 0.1f );
     }
 }
 #endif


A yellow sphere will show you what is producing this motion.

avatar image adamjamesnapier andrew-lukasik · Feb 15 at 02:12 PM 0
Share

I see the yellow sphere is indeed below my player, which is why the camera goes below the player, but I am not sure how to fix it. I do not see any properties for local positions for Transform and I do not see any position offsets being set. My front and back tires have a RigidBody2D and a circle collider and my car body has a RigidBody2D and a polygon collider. Not really sure if that info helps you or not. Sorry for the total newb questions lol I am just starting to learn Unity and I came from libGDX. The 2 are really like comparing apples and oranges haha. Let me know if you need any screenshots of the properties of the car within the editor. Thanks again for all the help man!

Show more comments

1 Reply

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

Answer by adamjamesnapier · Feb 16 at 11:10 PM

Well setting the camera position to the RigidBody2D.worldCenterOfMass mostly solved the issue, but the little bit of camera translation is hardly noticeable while you're moving. It is only if the car were to be stuck in place while spinning in which the camera movement would be noticeable. Glad I have solved it enough to fit my scenario. Thanks a bunch man, you're help was invaluable!

Comment
Add comment · Show 2 · 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 andrew-lukasik · Feb 17 at 12:24 AM 1
Share

You're welcome, glad it helped.

You can hide this remaining translation by replacing that rigid follow movement with a smooth one to introduce some inertia. Alternatively, creating a "dead zone" (as seen in Cinemachine camera system) could help too.

https://connect-prd-cdn.unity.com/20191216/learn/images/0c071449-c5be-407b-a73f-825ba1f9ff94_image1.png

avatar image adamjamesnapier andrew-lukasik · Feb 17 at 03:17 PM 0
Share

I like the dead zone idea! If I ever run into a scenario where I absolutely cannot have any camera movement upon player rotation, I will probably try that out.

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

133 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

Related Questions

Unity 5 Loading Screen Script? 3 Answers

A Question about custom splash screens 1 Answer

Merge static C# script and old java script function (NewtonVR) Oculus Touch 2 Answers

How to resolve this lightning error? 0 Answers

NPC Animation 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