Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dreave · Jun 07, 2012 at 11:18 AM · animationcamera

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ragnaros100 · Jun 13, 2012 at 03:55 PM

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();
     }
 }
 // [...]
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dreave · Jun 16, 2012 at 12:40 PM 0
Share

On the first script I have about 10 errors all saying "a namespace can only contain types and namespace declarations" what am I supposed to do?

avatar image whydoidoit · Jun 16, 2012 at 12:42 PM 0
Share

It's a partial script, you need to put it inside the behaviour class

avatar image Dreave · Jun 16, 2012 at 12:45 PM 0
Share

How do I do this?

avatar image Bunny83 · Jun 16, 2012 at 02:16 PM 0
Share

I've added the "class" around the code snippet.

avatar image
0

Answer by Bunny83 · Jun 16, 2012 at 02:08 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Blender Animation Problems 3 Answers

Unity 2D Shaking Player With Camera Damping 1 Answer

A node in a childnode? 1 Answer

Animation interference problem 1 Answer

Blender Animation in Unity Problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges