Camera animations?

I am trying to add animations to my main camera so that when my gun shoots, reloads or walks the camera either rotates or moves so that it looks more realistic. What I thought would work is that if I add an empty gameobject onto my player controller and then add my main camera onto that I could animate the empty gameobject with the unity animations window and write a script which would trigger these animations when certain buttons and keys are pressed.

This does not work.

I cannot seem to animate the empty gameobject that my main camera is attached to. I am trying to rotate it so that when I shoot it rotates the empty gameobject (and its children) up a little bit and back down to its starting position. I cannot add keys within the animation window it is greyed out, how could I stop this so I can animate my gameobject?

You CAN animate the camera by itself. But this approach is a very clumpsy way to create a recoil effect (which I am assuming that you’re doing)

I would recomend you to code the effect. It is much more precise and you can also easier customize it that way.

Here’s a very simple c# script which i wrote to make a recoil-like effect on your camera.

// C#
//RecoilScript.cs
using UnityEngine;
using System.Collections;

public class RecoilScript : MonoBehaviour
{    	
    //these variables are used to determine the angles n shit of the recoil of the weapon, on which the player 
    //are shooting with
    private Vector3 ang0;
    private Vector3 ang = Vector3.zero;
    private float targetX;
    public float force = 2.5f; //Controlls recoil amplitude
    public float upSpeed = 	9; // controls smoothing speed
    public float dnSpeed = 20; // controls how fast the weapon returns to original position
    
    void Update () 
    {
        //Recoil
        ang0 = transform.localEulerAngles; // Save the original angles.
        ang.x = Mathf.Lerp(ang.x, targetX, upSpeed * Time.deltaTime);
        transform.localEulerAngles = ang0 - ang; // move the camera.
        targetX = Mathf.Lerp(targetX, 0, dnSpeed * Time.deltaTime); //returns to rest
        //Recoil
    }
    
    public void Recoil ()
    {
        targetX += force;//Add the recoil force
    }
}

Just do a reference to the seperate script from your shooting script and tell the recoil script Recoil(); Like this:

// [...]
private RecoilScript recoilScript;

void Start ()
{
    recoilScript = cameraHeadTransform.GetComponent<RecoilScript>();
}

void Update ()
{
    if(Input.GetKeyDown(Keycode.A))
    {
        recoilScript.Recoil();
    }
}
// [...]

ragnaros100 is right. For such simple movements it’s usually easier to animate it via code. However to create an animation:

  • you need of course an animation component on that object or somewhere upwards on a parent.
  • Next thing you have to do is to create an AnimationClip in your assets and name it.
  • Then you have to add this animation clip to the animation component’s animation list.
  • If it’s part of that animation component, it can be selected in the animation window and of course be edited.

There are many tutorials out there that explain how the animation system works. Here’s one, not perfect but it should get you started.