I FORGOT TO SHOW THE PLAY MOTION METHOD BUT ITS SUPER SIMPLE Private void PlayMotion(Vector3 motion){ _camera.localPosition += motion; } YOU ALSO SHOULD PROBABLY CALL THE RESET METHOD SOMEWHERE ELSE LIKE BEFORE THE BOOLS IN CHECK MOTION INSTEAD OF UPDATE
@@inigowilloughby7285 sorry for the incredibly late reply, didn’t see this ): I’m sure you have resolved but in case anyone else needs, I would put it anywhere before our returns in check motion, so above or below where we set the local speed variable.
It's crazy. This was working great for me, but then I saved my project, reloaded it, and now I can't look up or down and moving forward makes my camera look up. I'm sure it has something to do with the lookat. As everything works when I disable the head bob script. Just weird.
Hey guys, if any of you are using a rigidbody controller then this code will work, In unity, unparent the camera holder from the player set CamPos to an empty game object of where you want the camera to be default using System.Collections; using System.Collections.Generic; using UnityEngine; public class ViewBob : MonoBehaviour { [SerializeField] bool Isenabled = true; [SerializeField, Range(0, 0.1f)] float amplitude = 0.015f; [SerializeField, Range(0, 30)] float frequency = 10f; [SerializeField] Transform cam = null; [SerializeField] Transform cam_holder = null; [SerializeField] Transform campos = null; float togglespeed = 1.0f; Vector3 startpos; Rigidbody controller; Movement movement; void Start() { startpos = campos.localPosition; controller = GetComponent(); movement = GetComponent(); } // Update is called once per frame void LateUpdate() { cam_holder.position = campos.position; cam_holder.position = campos.localPosition + cam_holder.position; CheckMotion(); cam.LookAt(FocusTarget()); } Vector3 FootStepMotion() { Vector3 pos = Vector3.zero; pos.y += Mathf.Sin(Time.time * frequency) * amplitude / 100f; pos.x += Mathf.Cos(Time.time * frequency / 2) * amplitude / 50f; return pos; } void CheckMotion() { float speed = new Vector3(controller.velocity.x,0f,controller.velocity.z).magnitude; ResetPosition(); if (speed < togglespeed) return; if (!movement.grounded) return; PlayMotion(FootStepMotion()); } void ResetPosition() { if (cam.localPosition == startpos) return; cam.localPosition = Vector3.Lerp(cam.localPosition, startpos, 1 * Time.deltaTime); } Vector3 FocusTarget() { Vector3 pos = new Vector3(transform.position.x, transform.position.y + campos.localPosition.y, transform.position.z); pos += cam_holder.forward * 15f; return pos; } void PlayMotion(Vector3 motion) { cam.localPosition += motion; } }
@@idkguy_. so he called his player movement Movement but you probably called it something else. You have no MonoBehaviour named "Movement" But you're written code that expects it to exist. I call my player movement PlayerMotor.
Everything works fine till I look either fully up or down, at which point the camera snaps to a weird rotation. Pretty sure it has something to do with the focus target, but not sure how to fix. Help would be appreciated.
For anyone who is doing this with a rigid body, all you have to do is call upon a rigid body instead of a character controller (private Rigidbody _controller;) then when it asks for the get component changed it too (_controller = GetComponent();), however i found out that you need to add your own "isGrounded" check so for that i added a Floormask ([SerializeField] private LayerMask FloorMask;) and a "feet"(the feet needs to be a empty object at the bottom of your player) transform ([SerializeField] private Transform FeetTransform;) once youve set the "ground" to the floor mask, go to the CheckMotion and change the "isGrounded" to a (if (!Physics.CheckSphere(FeetTransform.position, 0.1f, FloorMask)) return;) to put it simply this checks if your "feet" are touching the floormask and id they are then continue. hope this helps someone ¯\_(ツ)_/¯
Like others, I followed all the steps, and it doesn't work. No errors or anything, just no head bob. 3 hours or rewatching the same 1 minute clip darrting around with no context, double checking I missed some stupid typo rather than a 2 second pastebin. FML.
Sorry you had so much trouble, this video was definitely a bit of a mess lol. I have since started including the code for these projects and will try to come back / update these old ones.
Hey! Weird issue here, on project it works perfectly, but when I build the game, nope, zero headbob, tried rearranging script execution order but it effed it up more, any ideas what could it be?
My controller.velocity x and y seems to be 0 due to the script probably executing after Move function, which makes my headbob not trigger, how do i fix this?
Hey, I gotta quick question for ya. What theme do you use in Visual Studio? I know it's one of the base ones, but it looks nice and I don't enjoy the one I currently have.
for some reason. and this makes 0 sense at all The view bobbing works perfectly fine when I have the player selected in the hierarchy. but for some reason whenever I click off the player and deselect it. the view bobbing becomes heavily exaggerated. if anyone has any idea or whatever please let me know because this is baffling me.
for the people can't get it to work, try checking the speed variable at the CheckMotion function, my speed always return 0 which is smaller than toggleSpeed so the function return instead of run PlayMotion for head bob
I am having an issue of the Head Bob just going too much to the sides, i cant really think of a solution and it has been driving me insane. Could someone help me out on this one?
On the line: pos.x += Mathf.Cos(Time.time * _frequency / 2) * _amplitude * 2; you could multiply the _amplitude with a multiplier to dial down on the side movement
@@uvengine3219 Thanks for reply, I have tried doing that but the end result is that it would simply vary in different set ups anyway as Time.time is very much frame dependant, I have tried to get it to work with DeltaTime but it would just result in the camera sliding off to one side (it probably is possible to get it working but my knowledge isn't big enough). So at the moment my only solution is to limit the fps and work around that. Once again thanks for the reply!
it accesses the transform, so it will move what its attached to (camera). Make sure to also see my comment about the playmotion method, which I forgot to show. Sorry for the inconvenience :P
Sounds like your mouse and move axis inputs have been renamed from convention, just open up your input manager and check the names of each. I believe the default for mouse input is “Mouse X” and “Mouse Y”, move input is “Horizontal” and “Vertical”. But you can name them anything, just make sure you type in the appropriate name next to Input.GetAxis in code.
@@dispat odd, I don’t think a monobehaviour could function without being attached to a transform.. check around for an old or duplicate script you have attached to something?
@@hero3d899 apparently the problem was related to having the camera in the camera holder or something like that, now im trying a different approach on head bobbing, thanks forr the help tho :)
I just want to let people know that is thinking in incorporating this head bobbing thing which is a useless-dumb-infuriating feature in their games you’re gonna lose a big chunk of players and sales and its very well deserved, it’s not realistic by any means, myself I sometimes do some jogging and your eyes are always focused and doesn’t make this stup!d movement, your head will move but not your vision, but it’s up to you, keep adding this and you deserve to fail miserably, there’s a reason why the biggest fps avoid this dizziness inducing garbage like a plague.
This works perfectly, all it needed was multiplying deltaTime with the result of the footstep vector3, anyone who's getting compiler errors or no head bobbing at all by following a 1:16 video should probably give up coding 😀
Please for the love of god stop adding head bob to games. It's not realistic. Nobody's vision moves like that as they walk. It just makes some of us have motion sickness. Please stop.
@@toastystaken yes. Just like real life. There is no head bob. Also all of the animations in the world don't matter if a game gives me motion sickness.
@@aycicex You're most likely overriding the mouse input with the headBob. Create an empty object called HeadBob and try changing the position of it instead of the camera.
I FORGOT TO SHOW THE PLAY MOTION METHOD BUT ITS SUPER SIMPLE
Private void PlayMotion(Vector3 motion){
_camera.localPosition += motion;
}
YOU ALSO SHOULD PROBABLY CALL THE RESET METHOD SOMEWHERE ELSE LIKE BEFORE THE BOOLS IN CHECK MOTION INSTEAD OF UPDATE
@@inigowilloughby7285 sorry for the incredibly late reply, didn’t see this ): I’m sure you have resolved but in case anyone else needs, I would put it anywhere before our returns in check motion, so above or below where we set the local speed variable.
@@hero3d899 well, it still doesn`t work :/
@@hero3d899 it says that there is no definition for velocity please help
@@Llamu maybe you wrote it wrong
Works great! I’m using a rigid body instead so I just changed character controller to rigid body and it works nicely
Didn't know Cr1tical made Unity videos
I didn't know Cr1tical existed. I only know Cr1tikal.
Jokes aside, I like the Amon pfp
lmfao ikr, i thought the same thing
ikr
this dude sounds like he's gonna tell us our daily dose of internet
I've translated the tutorial code for an FPS game I'm making into Python and it works! Thank you very much.
Love the quick snappy instruction delivery... Makes me feel good inside. You've gotchurself a new subscriber!
Charlie makes Unity programming tutorials
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Headbob : MonoBehaviour
{
[Header("Configuration")]
[SerializeField] private bool _enable = true;
[SerializeField, Range(0, 0.1f)] private float _Amplitude = 0.015f; [SerializeField, Range(0, 30)] private float _frequency = 10.0f;
[SerializeField] private Transform _camera = null; [SerializeField] private Transform _cameraHolder = null;
private float _toggleSpeed = 3.0f;
private Vector3 _startPos;
private CharacterController _controller;
private void Awake()
{
_controller = GetComponent();
_startPos = _camera.localPosition;
}
void Update()
{
if (!_enable) return;
CheckMotion();
ResetPosition();
_camera.LookAt(FocusTarget());
}
private Vector3 FootStepMotion()
{
Vector3 pos = Vector3.zero;
pos.y += Mathf.Sin(Time.time * _frequency) * _Amplitude;
pos.x += Mathf.Cos(Time.time * _frequency / 2) * _Amplitude * 2;
return pos;
}
private void CheckMotion()
{
float speed = new Vector3(_controller.velocity.x, 0, _controller.velocity.z).magnitude;
if (speed < _toggleSpeed) return;
if (!_controller.isGrounded) return;
PlayMotion(FootStepMotion());
}
private void PlayMotion(Vector3 motion)
{
_camera.localPosition += motion;
}
private Vector3 FocusTarget()
{
Vector3 pos = new Vector3(transform.position.x, transform.position.y + _cameraHolder.localPosition.y, transform.position.z);
pos += _cameraHolder.forward * 15.0f;
return pos;
}
private void ResetPosition()
{
if (_camera.localPosition == _startPos) return;
_camera.localPosition = Vector3.Lerp(_camera.localPosition, _startPos, 1 * Time.deltaTime);
}
}
real hero
Thanks you saved me alot of time.
i love you
yes
@@arthurisdumb8820
WOW the most useful 1 min 16 seconds of my life. Thank you 😃
It's crazy. This was working great for me, but then I saved my project, reloaded it, and now I can't look up or down and moving forward makes my camera look up. I'm sure it has something to do with the lookat. As everything works when I disable the head bob script. Just weird.
same thing here, anyone has a solution?
BRO THATS CRAZY SAME THING HERE
Thanks this works great! it took just a little working out but I got it working now.
Hey guys, if any of you are using a rigidbody controller then this code will work,
In unity, unparent the camera holder from the player
set CamPos to an empty game object of where you want the camera to be default
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ViewBob : MonoBehaviour
{
[SerializeField] bool Isenabled = true;
[SerializeField, Range(0, 0.1f)] float amplitude = 0.015f;
[SerializeField, Range(0, 30)] float frequency = 10f;
[SerializeField] Transform cam = null;
[SerializeField] Transform cam_holder = null;
[SerializeField] Transform campos = null;
float togglespeed = 1.0f;
Vector3 startpos;
Rigidbody controller;
Movement movement;
void Start()
{
startpos = campos.localPosition;
controller = GetComponent();
movement = GetComponent();
}
// Update is called once per frame
void LateUpdate()
{
cam_holder.position = campos.position;
cam_holder.position = campos.localPosition + cam_holder.position;
CheckMotion();
cam.LookAt(FocusTarget());
}
Vector3 FootStepMotion()
{
Vector3 pos = Vector3.zero;
pos.y += Mathf.Sin(Time.time * frequency) * amplitude / 100f;
pos.x += Mathf.Cos(Time.time * frequency / 2) * amplitude / 50f;
return pos;
}
void CheckMotion()
{
float speed = new Vector3(controller.velocity.x,0f,controller.velocity.z).magnitude;
ResetPosition();
if (speed < togglespeed) return;
if (!movement.grounded) return;
PlayMotion(FootStepMotion());
}
void ResetPosition()
{
if (cam.localPosition == startpos) return;
cam.localPosition = Vector3.Lerp(cam.localPosition, startpos, 1 * Time.deltaTime);
}
Vector3 FocusTarget()
{
Vector3 pos = new Vector3(transform.position.x, transform.position.y + campos.localPosition.y, transform.position.z);
pos += cam_holder.forward * 15f;
return pos;
}
void PlayMotion(Vector3 motion)
{
cam.localPosition += motion;
}
}
What is the first movement in
Movement movement;?
@@idkguy_. so he called his player movement Movement but you probably called it something else. You have no MonoBehaviour named "Movement"
But you're written code that expects it to exist. I call my player movement PlayerMotor.
what is Campos?
doesn't seem to work.
It just doesn't work for me. There's no errors and I've been trying different stuff for a while now.
Everything works fine till I look either fully up or down, at which point the camera snaps to a weird rotation. Pretty sure it has something to do with the focus target, but not sure how to fix. Help would be appreciated.
use a mathf.clamp on the cam and the cam rotation should be done with * Time.fixedDeltaTime
it's still not working, when are you going to release the code? it's not that hard to copy paste it into your description
FINNALY A TUTORIAL FOR HEAD BOB
If moist critikal made tutorials
It didn't work for me. I checked my code over and over but it wouldn't compile for some reason.
For the first few seconds I thought you were Moist Cr1TiKaL
For anyone who is doing this with a rigid body, all you have to do is call upon a rigid body instead of a character controller (private Rigidbody _controller;) then when it asks for the get component changed it too (_controller = GetComponent();), however i found out that you need to add your own "isGrounded" check so for that i added a Floormask ([SerializeField] private LayerMask FloorMask;) and a "feet"(the feet needs to be a empty object at the bottom of your player) transform ([SerializeField] private Transform FeetTransform;) once youve set the "ground" to the floor mask, go to the CheckMotion and change the "isGrounded" to a (if (!Physics.CheckSphere(FeetTransform.position, 0.1f, FloorMask)) return;) to put it simply this checks if your "feet" are touching the floormask and id they are then continue. hope this helps someone ¯\_(ツ)_/¯
Man, this was very helpful! I was stuck with the same problem. Thanks a lot! :D
bro the FloorMask isnt WOrking on !_controller.Floormask) return
doesnt work for some reason
thanks man
Like others, I followed all the steps, and it doesn't work. No errors or anything, just no head bob.
3 hours or rewatching the same 1 minute clip darrting around with no context, double checking I missed some stupid typo rather than a 2 second pastebin. FML.
Sorry you had so much trouble, this video was definitely a bit of a mess lol. I have since started including the code for these projects and will try to come back / update these old ones.
@@hero3d899 Please do soon, love the video, but it just ain't working for me. :(
@@hero3d899 Its been 4 months could you pls include the code now
@@hero3d899 pleassssssssssssssssssse
Hey! Weird issue here, on project it works perfectly, but when I build the game, nope, zero headbob, tried rearranging script execution order but it effed it up more, any ideas what could it be?
@@bfrawg I think I did not fix it
void awake instead of start
can you do tutorial on landing camera effect where the camera goes down smoothly and after like 0.1 second it goes back to the position smoothly?
I think he's dead
My controller.velocity x and y seems to be 0 due to the script probably executing after Move function, which makes my headbob not trigger, how do i fix this?
is camera shaking also when you land after a jump with this script?
I followed everything and got no errors yet it still won't work.
Does anyone know what I might have done wrong?
me too
same
its probably because you might be using a rigidbody instead of a character controller or you havent made a movement contoller.
@@Hyphen3372 did it work to you bro?
This doesnt work for me . Can you help please?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeadBobController : MonoBehaviour
{
[SerializeField] private bool _enable = true;
[SerializeField, Range(0, 0.1f)] private float _amplitude = 0.015f;
[SerializeField, Range(0, 30)] private float _frequency = 10.0f;
[SerializeField] private Transform _camera = null;
[SerializeField] private Transform _cameraHolder = null;
private float _toggleSpeed = 3.0f;
private Vector3 _startPos;
private CharacterController _controller;
private void Awake()
{
_controller = GetComponent();
_startPos = _camera.localPosition;
}
void Update()
{
if (!_enable) return;
CheckMotion();
ResetPosition();
_camera.LookAt(FocusTarget());
}
private void PlayMotion(Vector3 motion)
{
_camera.localPosition += motion;
}
private void CheckMotion()
{
float speed = new Vector3(_controller.velocity.x, 0, _controller.velocity.z).magnitude;
if (speed < _toggleSpeed) return;
if (!_controller.isGrounded) return;
PlayMotion(FootStepMotion());
}
private Vector3 FootStepMotion()
{
Vector3 pos = Vector3.zero;
pos.y += Mathf.Sin(Time.time * _frequency) * _amplitude;
pos.x += Mathf.Cos(Time.time * _frequency / 2) * _amplitude * 2;
return pos;
}
private void ResetPosition()
{
if (_camera.localPosition == _startPos) return;
_camera.localPosition = Vector3.Lerp(_camera.localPosition, _startPos, 1 * Time.deltaTime);
}
private Vector3 FocusTarget()
{
Vector3 pos = new Vector3(transform.position.x, transform.position.y + _cameraHolder.localPosition.y, transform.position.z);
pos += _cameraHolder.forward * 15.0f;
return pos;
}
}
Check if the controller.isGrounded returns false or if the speed is lower than the toggle speed.
pretty sure you are missing some spaces in the serialize fields maybe idk
@@Llamu space in c# don't really matter actually
@@sirgwindortvinuel oh I didn’t know that
which terrian texture and lightning you use ?
Hey, I gotta quick question for ya. What theme do you use in Visual Studio? I know it's one of the base ones, but it looks nice and I don't enjoy the one I currently have.
Dark maybe
@@prilicat Huh, tried that a while back and no result
@@ImmoralKoala Sure enough I got dark theme working
@@lumeras115 For some reason I didn't even consider the date before posting, deleted it but damn you saw it fast lmao
how would you adapt this to work with a cinemachine virtual camera which has full control over the main cam?
nice tutorial moist critical
Super thank you!!
Thats MoistCritikal and no one can tell me otherwise
for some reason. and this makes 0 sense at all
The view bobbing works perfectly fine when I have the player selected in the hierarchy.
but for some reason whenever I click off the player and deselect it. the view bobbing becomes heavily exaggerated.
if anyone has any idea or whatever please let me know because this is baffling me.
i was mainly concerned about getting it back to the original position but after watching this video i realized i can just use lerp...
TY SO VERY MUCH MY POGCHAMP
i use an fps controller and it doesnt let me look down or up with the script help meh
This is funny yet useful at the sametime.
You are proobbabbllly the reason my game will stand out.
did everything same, doesn`t work for me :/
Im getting error code cs0116 any idea how to fix it?
not working but theres also no errors at the bottom of my screen pls help
Thank you so much. I use godot so i had to tweak around a bit but it works fine.
wouldnt be easyer to use an animation?
If it doesn't work for you, try changing float speed to new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).magnitude
no that just makes it worse
@@Llamu worked for me
@@romka9022 yes but as you can tell, it didn’t work for me
@@Llamu well it can depend on your player controller, input system etc.
@@romka9022 yeah that’s what I’ve been thinking
my camera goes away when I try to walk lmao
Pls tutorial how make this lighing and post processing
it doesn't work :/
thanks bro
!
can you put all of the script in the comments instead ?, thank you
Why not make the script public... I'll probably use an animation instead.
source code?
?
LookAt not working, FocusTarget Working
Head bob works fine, but I can't look left and right now, anyone have any ideas?
for the people can't get it to work, try checking the speed variable at the CheckMotion function, my speed always return 0 which is smaller than toggleSpeed so the function return instead of run PlayMotion for head bob
Where is PlayMotion() definition? I can't find it anywhere in the video!
Sadly this doesent work
can anyone please CTRL + C, CTRL + V the code in here for the head bob script? I'm Lazy.
Can you do that?
Awesome tutorial + you sound like MoistCritikal ;)
lmao ikr
I am having an issue of the Head Bob just going too much to the sides, i cant really think of a solution and it has been driving me insane. Could someone help me out on this one?
On the line:
pos.x += Mathf.Cos(Time.time * _frequency / 2) * _amplitude * 2;
you could multiply the _amplitude with a multiplier to dial down on the side movement
@@uvengine3219 Thanks for reply,
I have tried doing that but the end result is that it would simply vary in different set ups anyway as Time.time is very much frame dependant, I have tried to get it to work with DeltaTime but it would just result in the camera sliding off to one side (it probably is possible to get it working but my knowledge isn't big enough).
So at the moment my only solution is to limit the fps and work around that.
Once again thanks for the reply!
@@kam8527 A solution is to divide the motion Vector3 by a value, try 30-40-50.
0:59 How to make someone interested in stabilizing
can u make a vidio about jitter fix
thank a lot for the good tuto
So the camera stuff never references any object and so the code doesn't actually dose anything. How do I fix this?
it accesses the transform, so it will move what its attached to (camera). Make sure to also see my comment about the playmotion method, which I forgot to show. Sorry for the inconvenience :P
He did it 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥😋 thanks Bro
No problem fam, make sure to check my comment as I forgot some stuff
@@hero3d899 yeah thanks
ugh my brain
it appears like the controller is taking my mouse as an input instead of my keyboard, who knows how to fix this?
Sounds like your mouse and move axis inputs have been renamed from convention, just open up your input manager and check the names of each. I believe the default for mouse input is “Mouse X” and “Mouse Y”, move input is “Horizontal” and “Vertical”. But you can name them anything, just make sure you type in the appropriate name next to Input.GetAxis in code.
@@hero3d899 it has the right name, also the script gets applied even if its not attached to anything lol
@@dispat odd, I don’t think a monobehaviour could function without being attached to a transform.. check around for an old or duplicate script you have attached to something?
@@dispat or perhaps create a new test file and apply this code only to verify if the problem is coming from your current project
@@hero3d899 apparently the problem was related to having the camera in the camera holder or something like that, now im trying a different approach on head bobbing, thanks forr the help tho :)
Plz Put The Script In The Description!!!
For me it works but after some time in the game it just stops. any help?
Bro, idk with the people dont leave the fking code
Does this work with rigidbody?
I'm wondering the same thing, idk if it will cuz he uses Character Controllers
@@micr0wav3_able you can try
Bro pls link code
this shit is blursed. please don't stop.
you sound like moist critical lol
Moist critical????
*sigh*
I just want to let people know that is thinking in incorporating this head bobbing thing which is a useless-dumb-infuriating feature in their games you’re gonna lose a big chunk of players and sales and its very well deserved, it’s not realistic by any means, myself I sometimes do some jogging and your eyes are always focused and doesn’t make this stup!d movement, your head will move but not your vision, but it’s up to you, keep adding this and you deserve to fail miserably, there’s a reason why the biggest fps avoid this dizziness inducing garbage like a plague.
Серьезно? А где гайд то этот? ты показал только не которые отрывки скрипта!
Если полистать новые комментарии, там человек выложил рабочий код, правда мой персонаж не может почему-то двигать камеру по оси y
@@Danyaanl да мне уже не надо)
This works perfectly, all it needed was multiplying deltaTime with the result of the footstep vector3, anyone who's getting compiler errors or no head bobbing at all by following a 1:16 video should probably give up coding 😀
тханкс фро
Just use github next time.
Please for the love of god stop adding head bob to games. It's not realistic. Nobody's vision moves like that as they walk. It just makes some of us have motion sickness. Please stop.
so you want to walk around and feel like you have no animations whatsoever in video games? gotcha
@@toastystaken yes. Just like real life. There is no head bob. Also all of the animations in the world don't matter if a game gives me motion sickness.
@@SensualSquirrel real life has head bob, but most games overexaggerate it
are you a bird or something@@SensualSquirrel
it's far better than just a static camera, it makes games feel stiff without it.
it does the head bob but it doesn't focus, what shuold i do
when it focuses i connot move my mouse on y axes
@@aycicex You're most likely overriding the mouse input with the headBob. Create an empty object called HeadBob and try changing the position of it instead of the camera.