Make gun vibrate?

Talk about making games.

Make gun vibrate?

Postby tvz » Tue Mar 04, 2014 12:24 pm

Hello everyone,
I have a problem.
I want to make a rifle recoil, as it kickback when we shoot.
I dont want to use animation because it will tough for me to adjust many animations in one actor.
How can i make it do that with programming?
like in this image, the gun recoils only horizontally
Attachments
recoil.jpg
recoil like this
User avatar
tvz
 
Posts: 98
Joined: Sun Mar 10, 2013 7:13 am
Location: Your PC screen
Score: 3 Give a positive score

Re: Make gun vibrate?

Postby CrackedP0t » Wed Mar 05, 2014 2:49 am

You'll need a global int variable called "recoils" or something, and initialize it to -3.
I'm assuming that it will be part of a character that faces right and left, so make a "facing" int variable, and when the character faces left, set it to -1, and when it faces right, set it to 1.
You'll also need a timer called "recoiltimer" or something, that occurs 5 times.
When the shot is triggered, run this code in the gun actor:
Code: Select all
//Modify the "100" to make the kickback slower or faster.
CreateTimer("Event Actor", "recoiltimer", 100);

When the timer activates, run this code:
Code: Select all
x += sign(recoils) * facing * 10;
recoils++;
if ( recoils == 3) {
recoils = -3;
DestroyTimer("recoiltimer8");
                   }

}


Hope this helps!
/人 ◕‿‿◕ 人\
Fang Pictures, my game development company:
http://fangpictures.comuf.com
User avatar
CrackedP0t
 
Posts: 157
Joined: Mon Dec 30, 2013 5:49 pm
Location: Crested Butte, CO (The US of A)
Score: 8 Give a positive score

Re: Make gun vibrate?

Postby tvz » Wed Mar 05, 2014 8:17 am

Thanks
It worked :D
I modified it, changed the timer to 50 and set recoils to -1
+1



Just one question. What is the meaning of this line-
x += sign(recoils) * facing * 10;
User avatar
tvz
 
Posts: 98
Joined: Sun Mar 10, 2013 7:13 am
Location: Your PC screen
Score: 3 Give a positive score

Re: Make gun vibrate?

Postby CrackedP0t » Wed Mar 05, 2014 5:01 pm

No problem! :)

sign() means return -1 if the number is negative, 1 if the number is positive, and 0 if it's 0.

The line means:

Add to x:
10
times -1 if the number of recoils is negative, 1 if it's positive
times -1 if it's facing left, 1 if it's facing right.

So,
If number of times the recoil timer occurs is 6 (recoils = -3), the number of pixels it moves back is 10, and it's facing right, here's a timeline of the recoil:
  1. sign(-3) * 1 * 10 = -10
    x += -10
  2. sign(-2) * 1 * 10 = -10
    x += -10
  3. sign(-1) * 1 * 10 = -10
    x += -10
  4. sign(0) * 1 * 10 = 0
    x += 0
  5. sign(1) *1 * 10 = 10
    x += 10
  6. sign(2) *1 * 10 = 10
    x += 10
  7. sign(3) *1 * 10 = 10
    x += 10
So we're back where we started.

That should hep you, but just ask if it doesn't.
/人 ◕‿‿◕ 人\
Fang Pictures, my game development company:
http://fangpictures.comuf.com
User avatar
CrackedP0t
 
Posts: 157
Joined: Mon Dec 30, 2013 5:49 pm
Location: Crested Butte, CO (The US of A)
Score: 8 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest