- Home /
Head sway relative to players movement
I have been helping an artist friend work on a survial shooter game, and thought it would be cool to add a head sway, similar to modern first person survival horror games, rather than your traditional head bob. I have gotten it working fairly easily, but I'm afraid I'm missing something simple as it only works in relation to world coordinates rather than local coordinates. I.e., it only works while facing the z axis, whether negative or positive. I'd imagine this is due to me calculating it in world space, but i thought for sure localRotation would solve this issue but has not. I'm also guessing vodka has not been an integral part of formulating this properly from the get-go, lol.
using UnityEngine;
using System.Collections;
public class HeadSway: MonoBehaviour
{
public float SwaySpeed = 1f;
public float SwayMultiplier = 1f;
public CharacterController PlayerController;
float RadiansPerRevolution = 6.28318531f;
float SwayAngle = 0f;
float SwayRadial = 0f;
void Update(){
if (SwayRadial < RadiansPerRevolution) {
SwayRadial += SwaySpeed * Time.deltaTime;
}
if (SwayRadial > RadiansPerRevolution) {
SwayRadial -= RadiansPerRevolution;
}
SwayAngle = Mathf.Sin (SwayRadial)* SwayMultiplier * PlayerController.velocity.z;
transform.localRotation = Quaternion.Euler (transform.localRotation.eulerAngles.x, transform.localRotation.eulerAngles.y, SwayAngle);
}
}
Should also mention, I'm fairly ceratin that it's the way I'm inputing the character controllers z velocity. I'm guessing that returns only world space z velocity rather than local. If that is the case, is there an easy way to translate that into local coordinates?