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
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.
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!
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 theYou 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
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.
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!
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!
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.
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
Follow this Question
Related Questions
Hey all I am working on a 3D maze game. I would like to know how i could start and end the levels . 1 Answer
Do you need to have specific script types for different mobile platforms? 1 Answer
why do i keep getting compiler errors even though my script has no errors? 2 Answers
Picking up collectables in a correct (custom set) order to open gates? 0 Answers
Auto switch to legacy shader 0 Answers