- Home /
Trying to even a randomized rotation
So the title is a little unspecific but what I'm trying to do is create a head bob for my character using the camera's rotation. I'm using a modified version of the Unity standard asset HeadBob.cs.
using System;
using UnityEngine;
using UnityStandardAssets.Utility;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(CharacterMotor))]
public class HeadBobber : MonoBehaviour
{
public Camera Camera;
public LerpControlledBob jumpAndLandingBob = new LerpControlledBob();
private CharacterMotor motor;
public float HorizontalBobRange = 0.33f;
public float VerticalBobRange = 0.33f;
public AnimationCurve Bobcurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(0.5f, 1f),
new Keyframe(1f, 0f), new Keyframe(1.5f, -1f),
new Keyframe(2f, 0f)); // sin curve for head bob
public float StrideInterval = 5;
[Range(0f, 1f)] public float RunningStrideLengthen;
private bool m_PreviouslyGrounded;
private Vector3 m_OriginalCameraPosition;
// Use this for initialization
void Start ()
{
motor = GetComponent<CharacterMotor>();
m_OriginalCameraPosition = Camera.transform.localEulerAngles;
}
// Update is called once per frame
void Update ()
{
Vector3 newCameraPosition;
if(motor.movement.velocity.magnitude > 0 && motor.IsGrounded())
{
//float zPos = m_OriginalCameraPosition.z + (Bobcurve.Evaluate(Mathf.PerlinNoise(Time.time, 0))*VerticalBobRange) - VerticalBobRange/2;
//float zPos = m_OriginalCameraPosition.z + (Bobcurve.Evaluate(Mathf.PerlinNoise(Time.time, 0)) *VerticalBobRange) - 2.325f;
float zPos = Bobcurve.Evaluate(UnityEngine.Random.Range(-1f, 1f)) * VerticalBobRange;
Camera.transform.localEulerAngles = new Vector3(Camera.transform.localEulerAngles.x, Camera.transform.localEulerAngles.y, zPos);
newCameraPosition = Camera.transform.localEulerAngles;
}
else
{
newCameraPosition = Camera.transform.localEulerAngles;
}
Camera.transform.localEulerAngles = newCameraPosition;
}
}
This all works except that the value that Bobcurve.Evaluate() returns is only a positive float value and I need to correct it because 0 is straight up and I need the head to bob both right and left not just left. I've tried a few methods to correct it, subtracting by half the bobRange, and by half the Perlin Noise range (which should be 0.5), and by half the effective range of the bob (the bob's range does not equal the difference between the high and low, not sure why just what it seems to do). I can get one of two outcomes, I can either get an equal range on both sides of 0 (-1.6 - +1.6) but only 1 in 10 frames is on the negative side, or I get 50/50 positive and negative but the range is predominantly positive (-0.5 - +2.5). Also using the Random.Range makes the camera jitter back and forth.
Does anyone have any suggestions to combine the two outcomes? I would like to be able to use the Perlin Noise to generate a smooth bob that has an even distribution of outcomes in both maximums and odds.
This seems like a strange way to approach the requirement. The terms in which you're speaking tell me your approach is not as intuitive as it might be.
Creating the bob behavior is a technical pursuit, while tailoring that behavior is an artistic one. In situations like this, it's wise to think like an artist; ensure that your solution to the task works in an intuitive way.
For mechanical things, this often involves helper empties; building a mechanical rig of objects, each of which can be manipulated easily in local space.
This approach has the added benefit of giving you much more control. For instance, your "head motion" rig could also have behaviors for looking at a specific object, shaking the player's head yes or no, shaking as if in an earthquake, etc. You can make the rig independent of the player's $$anonymous$$ouseLook behaviors by operating on the rig in the rig's local space, then or temporary and/or partial control of the $$anonymous$$ouseLook for certain behaviors.
Also, you're having to work with euler angles, and specific hardcoded values and ranges. This is usually also a good sign you could tailor your solution to be a little more intuitive. Leverage helper objects, vector math, and the vector and quaternion class methods.
WRT your original question, I don't grasp why you'd want to use a random range or perlin noise to achieve anything here. A traditional head-bob motion would not feature randomness manifested in the way you're trying to use it. Perhaps the extents or rates might be pre-randomized for each cycle, but during the cycle, I cannot imagine random values producing desirable results. To get any randomness here, using noise strikes me as major overkill. I'd try to dig in and help more, but I didn't really grasp what you're doing.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201127im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
triggering random animations with gui 1 Answer
two Random.Range returning similar values 1 Answer
Making a camera list 1 Answer
C# Randomize Background.Color Issues 1 Answer
Missing a Method? Random.Range? 1 Answer