- Home /
Need Help With Animations! In C#
So I A Complete Newbie At Unity, But I have this problem, I have my rifle, and a couple Animations On It: Aiming down Sights Shooting Without Aiming Down Sights Shooting With Aiming Down Sights
Shooting With Aiming Down Sights Is Just The last key frame from The Aiming Down Sights Animation, But With A recoil Effect, But when i detect input and Play the Aiming Down Sights Shooting Animation While i'm aiming down the sights of course, It does this weird pull down thing, This Is My Code, Basically what i want to accomplish is a smooth recoil effect from my previous animation public class AnimPlays : MonoBehaviour {
public bool isSighted = false;
void Update ()
{
if(Input.GetButtonDown("Fire1") && isSighted == false)
{
animation.Play("m1shoots");
Debug.Log("fired_shot");
}else if (Input.GetButtonDown ("Fire1") && isSighted == true)
{
animation.Stop();
animation.Play("m1shootssighted");
Debug.Log("fire_shot");
}
if(Input.GetButton("Fire2"))
{
isSighted = true;
}else
{
isSighted = false;
}
if(isSighted == true)
{
animation.Play("m1s");
}
if(isSighted == false)
{
animation.CrossFade("idlem1");
}
}
Much Apreciated, Cheers!
Answer by kewur · Jan 24, 2014 at 07:18 PM
Normally what you want to do when shooting a rifle like object, you want the animation to be on the humanoid character (if that's what you have) and mount the rifle to it's hand bone. If you are new to Unity and game programming I would suggest doing it that way because unity handles smooth transitioning between animations for you. You should consider using Mecanim animations.
For example, if you want a recoil effect on your character you would have 2 animations with maximum recoil effect and zero recoil effect and blend them by using Mecanim's Blend tree. You can than give a value to each weapon in your game called "Recoil" between values 0 and 1 and feed it to Mecanim.
But if you insist on using legacy animations, you are going to have to develop your own Blending. Which is not trivial but there are some tutorials out there about the subject on 3Dbuzz.
Hope this helps.
If I Used The Arms To Simulate Recoil, Would I Do That In Unity Or An External Animation Program Like Blender?
Answer by IndieScapeGames · Jan 24, 2014 at 04:54 PM
I find it better to use AnimationClips than Animations.
Make several public AnimationClip parameters, and assign the animations to each one.
Then when you call animation.CrossFade(animationParamater.name)
The '.name' is important because it references the string value bane of the animation instead of hardcoding the animation names.