Make a character controller shake.

Hi boys and girls, I have a fairly complex player_movement script that I wrote, myself. It utilises unity’s character controller component.

I basically want to get characters hit by “special power beams” to stop responding to gravity and player input, and shake wildly on-the spot.

locking off gravity and player movement I can manage, but what’s a good way to get character controller to shake? I know how to pass a vector3 into controller.move, like:


but not sure how to use this to get a shaking effect. Any thoughts on an efficient way to do it? I am currently thinking I would create two lerp movements, one that interpolates between left and right movement and one for up and down movement, at varying speeds they could possibly get a good shaking effect?

a good or bad way to do it?

Hey i leave you one script i made for a camera shake effect that is applied when a big spell explodes, maybe it gives you some ideas. It worked for me really nice you can twitch the timing and it has a linear ease down it starts at the maximum shake power and it decrease by the amount you want.

private var ShakeAmount : float;
private var ShakeDuration : float;
private var TimeShaking : float;
private var DefaultPositionX : float;
private var DefaultPositionY : float;
private var ShakeSlow : float;

function Start () {
	DefaultPositionX = transform.localPosition.x;
	DefaultPositionY = transform.localPosition.y;

function Update () {
	if (TimeShaking <= ShakeDuration) {
	transform.localPosition.x = Random.Range(-ShakeAmount , ShakeAmount);
	transform.localPosition.y = Random.Range(-ShakeAmount /3 , ShakeAmount/3);
	TimeShaking += Time.deltaTime;
	ShakeAmount -= ShakeSlow;
	} else {
		transform.localPosition.x = DefaultPositionX;
		transform.localPosition.y = DefaultPositionY;

function ShakeCamera(ExpPower : float, ExpDuration : float){
	ShakeAmount = ExpPower;
	ShakeDuration = ExpDuration;
	TimeShaking = 0.0;
	ShakeSlow = ShakeAmount / (ShakeDuration / Time.deltaTime);

============== EDIT ==============
Sorry i have to clarify i made this for a 2D game thats why i move the x and y position and not the rotation, but you can adjust that to your convenience. Hope it helps

if you don’t want the entire character to shake you can implement the script inside of the “Main Camera” that way instead of causing possible errors when your character is moving around you can just shake the camera thus giving the same illusion. Sorry if this really didn’t help I’m fairly new to unity :s