Can some one please explain this scrip to me :)

Hello guys can you explain this script to me please :

if(headBobEnabled) {
		
		var percentage = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
		
		
		var desiredPosX = initialLocalPos.x + headBobDistance.x * Mathf.Sin(Time.time * headBobSpeed + Mathf.PI/2);
		//Blend between stationary and desired x position
		t.localPosition.x = (initialLocalPos.x * (1 - percentage)) + (desiredPosX * (percentage));
	
}

i want to know what the Mathf.PI/ 2 is doing and what
var percentage = Mathf.Min(1, Mathf.Abs(Input.GetAxis(“Vertical”)) + Mathf.Abs(Input.GetAxis(“Horizontal”)));

:slight_smile: what is 1 needed for right before the mathf.min? please explain this in a simple way if possible thank you :slight_smile:

2 Answers

2

Okay. Starting with your first question, Mathf.Pi/2 is a value in radians for 90-degrees. This is important in this case since the mathematicial function Sin repeats every 2Pi. This is important because without the offset, sin would return 0 at time 0. With this setup, it will return 1 at time zero.

The answer to your second question is caused by the nature of the math you are doing. It simply gets a speed that is between 0 and 1, and if the speed is greater than 1 (i.e. if you have a 60% vertical and a 50% horizontal deflection), it reduces the total sum to one. Clamping the value to between 0 and 1 would be more clear.

Thank you that actually make sense :) thank you very much

percentage = Mathf.Min(1, Mathf.Abs(Input.GetAxis(“Vertical”)) + Mathf.Abs(Input.GetAxis(“Horizontal”)));

Is used to cut the percentage to the maximum of 1. I.e. if “Vertical+Horizontal” is more than 1 then “Min” will return “1”.

Mathf.PI/ 2

Is used to shift the “Sin” value. Half of PI is equal to 90 degrees. In general “Sin” here is used as some coefficient to position. To shift desired position smooth.

thank you for a great answer :) means alot :)