x


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

more ▼

asked Nov 15 '10 at 03:43 PM

oliver-jones gravatar image

oliver-jones
2.5k 205 225 254

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Nov 15 '10 at 04:11 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

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.

Nov 15 '10 at 05:35 PM oliver-jones

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);

Nov 15 '10 at 07:20 PM skovacs1
(comments are locked)
10|3000 characters needed characters left

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.

==

more ▼

answered Nov 15 '10 at 04:15 PM

equalsequals gravatar image

equalsequals
4.5k 16 28 64

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

Nov 15 '10 at 05:34 PM oliver-jones

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.

Nov 15 '10 at 08:44 PM equalsequals
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3778
x1279
x195
x24
x19

asked: Nov 15 '10 at 03:43 PM

Seen: 1349 times

Last Updated: Nov 15 '10 at 04:00 PM