Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Pure Phase · Apr 30, 2010 at 10:06 AM · animationfpsgun

How can I animate my gun when I'm moving

I'd like to make my gun bob up and down a bit on my fps whilst I'm moving. Could someone explain the basic way I would go about doing this? I've got an animation that plays when I fire so I know a little bit about it but I'm not quite sure on how to find out when the player is moving in code. Thanks

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
2

Answer by spinaljack · Apr 30, 2010 at 10:29 AM

If you've already got your bob animation in the gun then all you need to do is add another script to the gun that says something like

if(Input.GetKey("w"){
  animation.Play("walk forward");
}

this might be a problem if you want to walk forward and shoot at the same time so add an else clause to only play the bob animation when not shooting.

Alternatively you can adjust the position of the gun using a sin wave adjusting frequency and amplitude based on movement speed.

something like:

transform.localPosition = Vector3(0, bobHeight*Mathf.Sin(bobSpeed), 0);

bob speed needs to be incremented every frame for a smooth motion and you need to centre the gun when you stop.

You can also do side to side motion or twisting much the same way.

The advantage of this method over animation is that it's directly linked to movement speed but on the other hand it's more fiddly to get it to look right.

EDIT

Using your example:

function Update () {
   if (Input.GetButton("Fire1")) {
      animation.Play("AnimationEnfield");
   } else if (Input.GetKey("w") {
      animation.Play("AnimationEnfield2");
   }
}

You can also use animation blends to transition between animations

Comment
Add comment · Show 1 · 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 Pure Phase · Apr 30, 2010 at 11:59 AM 0
Share

Thanks, I have tried the first way and like you say, it messes up the fire animation. I tried to write my own code but I think i've got it badly wrong. Here is my attempt:

var fireclip;

function Update () {

if (Input.GetButton("Fire1")) { animation.Play("AnimationEnfield"); fireclip = true; return;

}

if(Input.Get$$anonymous$$ey("w") && fireclip == false){ animation.Play("AnimationEnfield2"); } else { return; } }

The fire animation still works but the walking one doesnt - I'm not sure how to do this but I'm pretty sure I've got it completely wrong!

avatar image
0

Answer by Pure Phase · Apr 30, 2010 at 12:00 PM

Sorry, I'll put that as an answer so it's easier to read

var fireclip;

function Update () {

if (Input.GetButton("Fire1")) { animation.Play("AnimationEnfield"); fireclip = true; return;

}

if(Input.GetKey("w") && fireclip == false){ animation.Play("AnimationEnfield2"); } else { return; } }

Comment
Add comment · Show 3 · 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 spinaljack · Apr 30, 2010 at 01:05 PM 0
Share

does your fireclip ever become false? add and else after the if statement with fireclip = false

avatar image spinaljack · Apr 30, 2010 at 01:10 PM 0
Share

better to edit your question rather than post another answer

avatar image Pure Phase · Apr 30, 2010 at 02:20 PM 0
Share

Noted, I wondered how I should respond.

I think I've got it working now thanks!

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

No one has followed this question yet.

Related Questions

How to add gun to the model, as when it animate, the gun is dis-positioned. 2 Answers

Animations have been cleared, help! 0 Answers

FPS gun movement 1 Answer

Select Object ( Weapon ) Quick! Help! xD 1 Answer

FPS Aim Down Sights. Exact Position. 1 Answer


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