- Home /
Random Rotation To Animation HELP!
I have an animation on my rifle that is basically a recoil (kickback) that it triggered every time the 'Fire1' is pressed. But because it is playing the same recoil animation over and over again it looks a little bit 'fake'.
It is possible to add a slight random rotation animation to the recoil animation, so every time the recoil is played, the rotation differs slightly.
Now, currently I have got a script that changes the rotation randomly, but its not animation - so it looks like its 'snapping' (doesn't look smooth):
var SIG : Renderer;
function Update() {
if (Input.GetButton ("Fire1")) { animation.wrapMode = WrapMode.Clamp; animation.Play("SIG_fire", PlayMode.StopAll); SIG.transform.localRotation = Quaternion.AngleAxis(Random.value * 0.5, Vector3(1, 1, 0)); } }
SIG is the gun model.
Regards, Ollie
Answer by skovacs1 · Nov 15, 2010 at 04:11 PM
To get a smoother result, you could do something like adding one or more secondary animations designed to sync up with your current recoil animation and act on top of them with layers, using a synclayer if necessary and setting weight or using Blend to randomly set the effect of your offset on top of your recoil.
The correct approach would be to have your recoil be based on your gunshot direction and could be done in several different ways from separate animations for different gunshot positions or simplifying your recoil animation or using physics or driving that part of the animation in code.
What exactly are "synclayer and Blend"? I'm not familiar with them, if you could give an example, (or find a video example) that would be great. Your concept does sound like a good idea.
Layers are for animations to happen simultaneously on top of each other and allow you to weight them. You can specify an animation's layer with something like animation["recoilAdjust"].layer = 1. SyncLayer would be used to sync up animations on a given layer which may not be necessary in your case. Blend is an automated interface to smooth in your animation weighting like Lerping your animation weight, but you could set the weight explicitly in the animationstate as equalsequals suggests with something like animation["recoilAdjust"].weight = Random.Range(0.0f,1.0f);
Answer by equalsequals · Nov 15, 2010 at 04:15 PM
My suggestion is to look into using AnimationState layering. You could have a few AnimationClips applied and running simultaneously and weight the layers differently to achieve 'natural' results.
For example:
Have your standard recoil AnimationClip on layer 1, and then set another animation clip which controls only the rotation on layer 2. This rotation should be the maximum in which you want to rotate. From there, just before you tell these animations to play, you randomly choose a value between 0 and 1 and assign that to the weight of the rotation animation (layer 2). The blending that occurs will take into account the weight and should give you slightly randomized recoil which is unique every time.
Note: This is a very basic explanation and I am likely missing a couple details, but that should give you the general idea of where to look/start.
Hope that helps.
==
I'm not familiar with AnimationState - infact, I'm kinda new to Unity anyway. What are Layers? and how do I apply animation to them?
Sorry if thats a really newby question.
Also what do you mean by weight.
Please, do go into detail lol I sure would appreciate it
It's an advanced topic for sure, if you're new to Unity or 3D in general it might be a little difficult to grasp. Let me try and revise my answer and see if I can't help.