- Home /
Headbobber script in C#?
http://unifycommunity.com/wiki/index.php?title=Headbobber
Does anybody have a camera bob script like the one I posted above, but in C#? I am fairly new and I have no idea how to convert it. I planned on adding onto it with different bobbing speeds for how fast my character is sprinting, but I do not know JavaScript at all. I also need to access variables from another C# script to get my run speed, and I'm not sure if you can do that in JS either...
public class CustomHeadBob : MonoBehaviour
{
[SerializeField, Range(0.5f, 15f)] float _Xperiod = 5f;
[SerializeField, Range(0.5f, 15f)] public float _Yperiod = 8f;
[SerializeField, Range(0.05f, 0.5f)] public float _Xampl = 0.15f;
[SerializeField, Range(0.05f, 0.5f)] public float _Yampl = 0.1f;
[Header("Camera")]
[SerializeField] Transform _cameraTransform;
[Header("Character")]
[SerializeField] Transform _characterTransform;
private Vector3 _startPos;
private Vector3 _oldPos;
private float _travelled;
private void Start()
{
_startPos = _cameraTransform.localPosition;
_oldPos = _characterTransform.position;
}
protected void FixedUpdate()
{
UpdateTravelled();
Vector3 offsetVec = GetOffsetVec();
_cameraTransform.localPosition = _startPos + offsetVec;
}
private Vector3 GetOffsetVec()
{
var sinX = _Xampl * Mathf.Sin(_Xperiod * _travelled);
var sinY = _Yampl * Mathf.Sin(_Yperiod * _travelled + 50f);
return new Vector3(sinX, sinY, 0);
}
private void UpdateTravelled()
{
_travelled += (_characterTransform.position - _oldPos).sqrMagnitude;
_oldPos = _characterTransform.position;
}
}
Answer by Muuskii · Jul 12, 2012 at 02:11 AM
Looks like this would be a converted version:
public class Headbobber: MonoBehaviour
{
private float timer = 0.0f;
float bobbingSpeed = 0.18f;
float bobbingAmount = 0.2f;
float midpoint = 2.0f;
void Update () {
float waveslice = 0.0f;
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 cSharpConversion = transform.localPosition;
if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) {
timer = 0.0f;
}
else {
waveslice = Mathf.Sin(timer);
timer = timer + bobbingSpeed;
if (timer > Mathf.PI * 2) {
timer = timer - (Mathf.PI * 2);
}
}
if (waveslice != 0) {
float translateChange = waveslice * bobbingAmount;
float totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
totalAxes = Mathf.Clamp (totalAxes, 0.0f, 1.0f);
translateChange = totalAxes * translateChange;
cSharpConversion.y = midpoint + translateChange;
}
else {
cSharpConversion.y = midpoint;
}
transform.localPosition = cSharpConversion;
}
}
It's hard to know because it seems undefined variables pop up out of no where. Unfortunately I don't have any projects to test this code for you, but the console doesn't give any compile errors for me. Also, the cSharpConversion V3 is there because c# doesn't like transform.localPosition.y = midpoint; :(
Happy coding.
This seems pretty much the same as the JS one, I tested it quickly and it works! Thank you.
Thank you for accepting my answer. It's no problem really I was just banging my head from my own project not working anyway xD
didn't work, how? if your weapon 'disappears' it's probably because its position is off camera, i.e. the midpoint variable is too large. $$anonymous$$ake this and the bobbingSpeed and bobbingAmount variables public and you can tweaks things during runtime. With my model I found that the midpoint offset could stay at zero.
Answer by thatCamelCode · Oct 12, 2015 at 05:42 PM
I know this is a really... really... old post. But, I was pokin' around here and I thought I would add my own class that I made for head bob. It's slightly different in that it adds bob along the x-axis, giving the bob a parabolic path (a bob style I find more realistic than just up and down). I hope this helps someone else who's just pokin' around and wants a different bob style :)
public class Headbob : MonoBehaviour {
public Vector3 restPosition; //local position where your camera would rest when it's not bobbing.
public float transitionSpeed = 20f; //smooths out the transition from moving to not moving.
public float bobSpeed = 4.8f; //how quickly the player's head bobs.
public float bobAmount = 0.05f; //how dramatic the bob is. Increasing this in conjunction with bobSpeed gives a nice effect for sprinting.
float timer = Mathf.PI / 2; //initialized as this value because this is where sin = 1. So, this will make the camera always start at the crest of the sin wave, simulating someone picking up their foot and starting to walk--you experience a bob upwards when you start walking as your foot pushes off the ground, the left and right bobs come as you walk.
Vector3 camPos;
void Awake()
{
camPos = transform.localPosition;
}
void Update()
{
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) //moving
{
timer += bobSpeed * Time.deltaTime;
//use the timer value to set the position
Vector3 newPosition = new Vector3(Mathf.Cos(timer) * bobAmount, restPosition.y + Mathf.Abs((Mathf.Sin(timer) * bobAmount)), restPosition.z); //abs val of y for a parabolic path
camPos = newPosition;
}
else
{
timer = Mathf.PI / 2; //reinitialize
Vector3 newPosition = new Vector3(Mathf.Lerp(camPos.x, restPosition.x, transitionSpeed * Time.deltaTime), Mathf.Lerp(camPos.y, restPosition.y, transitionSpeed * Time.deltaTime), Mathf.Lerp(camPos.z, restPosition.z, transitionSpeed * Time.deltaTime)); //transition smoothly from walking to stopping.
camPos = newPosition;
}
if (timer > Mathf.PI * 2) //completed a full cycle on the unit circle. Reset to 0 to avoid bloated values.
timer = 0;
}
}
P.S. My default values give a subtle head bob, but you could easily make it more or less dramatic to fit your needs by just adjusting bobSpeed and bobAmount through the inspector :3
how to make it working with gun object ? Please ... my camera It is located at coordinates 0, not in place before start! @@ my camera inside other object @
Uh hi, I was just looking around for realistic headbobbing, and I found this. But Idk why your script doesn't work [having no effect on the camera] no matter whether I attach it to camera or an empty object which is its child or whatever values I use. But there are no compiler errors Any help on this problem please? I know its a bit late but I saw this post recently and I am a new user.
replace this
Vector3 camPos; = public Transform camera; camPos = newPosition; = camera.localPosition = newPosition; camPos = new position; = camera.localPosition = newPosition;
Answer by Spy-King · Sep 12, 2016 at 04:09 PM
I changed your script a little. I think it works better and is more optimized, but thanks for actually sharing the code. I've been searching for exactly that for hours! :)
Here is my script -
public bool headBob; public float headBobbingSpeed; public float headBobbingAmount; public float transitionSpeed; public Vector3 restPosition; private float timer; void PerformHeadBob() { %|-586279682_8|% %|423938851_9|% { %|1116776235_11|% //use the timer value to set the position %|1148472010_13|% restPosition.y + Mathf.Abs((Mathf.Sin(timer) * headBobbingAmount)), restPosition.z); //abs val of y for a parabolic path targetPos = newPosition; } else { timer = Mathf.PI / 2; //reinitialize targetPos = restPosition; } %|-1331604136_22|% timer = 0; %|1689128284_24|% cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, targetPos, transitionSpeed); }
Comment if you cannot understand why I use a particular variable.
ummm.. I'd really like to see your code, but you didn't use the option to insert a code sample... so it's not quite readable...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Camera Lean 0 Answers
Distribute terrain in zones 3 Answers
Instantiated GuiTextures not showing 1 Answer
MirrorReflection Portals 0 Answers