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 Flipbee9 · Jul 07, 2010 at 08:33 PM · gunmovingbobbing

Making A Gun "Bob" While Moving

So, I'm putting together an FPS, and everything's now working pretty good, but now, I want to know, what's a good way to have a gun that "bobs" while moving. So, while I"m running, the gun will shake a bit as I run. Animations would be good, but I'm more comfortable doing it in code. What's the best way to do it?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Jason_DB · Jul 07, 2010 at 09:36 PM

What I do is transform between two "sway points" to get the appearance of walking. When I reach point 1, i start going towards point 2 and vice versa. I call this function while the player is moving.

function walkSway () {
    if(swayTarget == 1){    
            //swayTarget is which of the two points I'm going towards
         if (Vector3.Distance(transform.localPosition, walkSway1) >= .01){
            curVect= walkSway1 - transform.localPosition;
                     //if the gun isn't at sway point one, transform towards it (the speed at which it transforms depends on the speed of the player) .
            transform.Translate(curVect*Time.deltaTime*swayRate*player.GetComponent("FPSWalker").speed,Space.Self);
        } else {
                    //if it has reached sway point 1, start going towards sway point 2
            swayTarget = 2;
        }
    } else if(swayTarget == 2) {
        if (Vector3.Distance(transform.localPosition, walkSway2) >= .01){
            curVect= walkSway2 - transform.localPosition;
                            // curVect is just the temporary vector for the translation
            transform.Translate(curVect*Time.deltaTime*swayRate*player.GetComponent("FPSWalker").speed,Space.Self);
        } else {
            swayTarget = 1;
        }
    }
}

I define the 2 sway points at start based on my "sway factor", which is a vector3. It is the distance (x, y, and z) which I want the gun to sway by. ( I call this function in Start())

function defineSwayPoints () {
    walkSway1 = transform.localPosition + swayFactor;
    walkSway2 = transform.localPosition - swayFactor;

}

I also have a function to return the gun to normal position when not walking:

function resetPosition () {
     if (transform.localPosition != startPosition){
        curVect= startPosition - transform.localPosition;
        transform.Translate(curVect*Time.deltaTime*2,Space.Self);
     }

}

Then all you need to do if you have those functions is put this in Update or LateUpdate:

function LateUpdate() {
if(FPSWalker.walking){
    walkSway();
} else {
    resetPosition();
}

}

And this into the FPSWalker script:

static var walking : boolean = false;

//add this into FixedUpdate() if((Mathf.Abs(moveDirection.x) > 0) && grounded || (Mathf.Abs(moveDirection.z) > 0 && grounded)){ //if the player is moving, walking is true if(!walking){ walking = true; } } else if(walking){ walking = false; }

Comment
Add comment · Show 5 · 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 Flipbee9 · Jul 07, 2010 at 09:52 PM 0
Share

Thanks! This helps me a lot

avatar image Flipbee9 · Jul 07, 2010 at 10:39 PM 0
Share

I'm curious about the swayFactor, am I supposed to set that up as a variable?

avatar image Jason_DB · Jul 07, 2010 at 11:26 PM 0
Share

Yeah, it's a Vector3 variable on the script with the sway stuff in it.

avatar image DuckieMonster · Jul 05, 2011 at 05:46 PM 0
Share

I'm so sorry for this but i'm trying to achieve the same thing and have no idea how to implement this JavaScript code...i'm trying to learn program$$anonymous$$g and i find it really interesting but at the moment i'm kinda stumped...could use a hand :-/,

avatar image Lewenos · Feb 27, 2012 at 07:02 PM 1
Share

Can you send us the complete script that we can look up the variables and get the ready script. :)

If you could be so nice. :D Thanks

-Lewenos

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

2 People are following this question.

avatar image avatar image

Related Questions

How to make a gun bob 1 Answer

moving gun in first person shooter while walking 8 Answers

How to position a gun on a FPS Charater 1 Answer

FPS Scripting Problems 2 Answers

Some issues with weapon switching and reloading? 2 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