- Home /
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:
controller.move(moveDirection);
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?
You can make use of the Random class to create random values you interpolate to by using lerp. But I would consider to shake only the camera. It usually (game depended) causes a cool shaking effect and it is less error prone as it doesn't cause any changes in the game logic and only changes the players view.
ah using random is a cool idea. I'm making a 2D fighter with all players sharing the screen so if i do camera shake i may make it really subtle, but want characters to spaz when they get hit by power beams xD
Answer by podmaster · Nov 06, 2014 at 12:05 AM
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
that's cool, the game I'm making is 2D as well. I think if i tweak this a little It will suit my needs, thanks man!
Glad to help, remember to check the answer for the future if it worked ;) Cheers
Answer by Haunter894 · Nov 05, 2014 at 11:39 PM
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
not too worried about errors in my character motion, I can sort that out just fine, just getting the shake to happen in the first place is where I'm gettin stuck.
Your answer
Follow this Question
Related Questions
interpolate a move variable 0 Answers
Player Animation and control panel 2 Answers
Help: A collider attached to the character controller of a FPS does not collide 1 Answer
Character Controller Script 3 Answers
Controlled sphere/ Ball game 2 Answers