Bow and Arrow in the Blender Game Engine

Опубликовано: 17 Октябрь 2024
на канале: CG Masters
40,219
289

VERSION UPDATE: There's a bug in latest blender versions that makes the arrow fall straight down when you pull the string all the way back. The problem was that when the pull animation reaches the last frame, it says it's on frame 0 for some reason, so 0 velocity is applied to the arrow. To work around this I've added 4 lines after the "else" statement that sets the frame number to 40 if it returns as 0, and I changed the launcher.actuators line to use the variable "frame" defined by those 4 new lines. You can see it in the updated script below. Couldn't figure out code formatting on youtube, so just replace [tab] tags with actually tabbing in there once or twice.

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
pull = cont.actuators["pull"]
fire = cont.actuators["fire"]
space = cont.sensors["space"]
launcher = bge.logic.getSceneList()[0].objects["arrow_launcher"]

if space.positive:
[tab]pull.frameStart = 1
[tab]pull.frameEnd = 40
[tab]cont.activate(pull)

else:
[tab]if pull.frame == 0:
[tab][tab]frame = 40
[tab]else:
[tab][tab]frame = pull.frame
[tab]launcher.actuators["add_arrow"].linearVelocity = [-(frame * 15), 0, 0]
[tab]pull.frameStart = 41
[tab]pull.frameEnd = 46
[tab]cont.activate(pull)
[tab]cont.activate(fire)

This is the second part of the bow and arrow tutorial series. Check out the first part(rigging a bow) at    • Bow and Arrow Rigging in Blender  

Download the start file for this tutorial here: http://www.cgmasters.com/tutorials/bo...

Download the final game here(textures won't display in 2.75, but game logic works): http://www.cgmasters.com/tutorials/bo...

Check out https://cgmasters.com for more courses and tutorials.

Like the tutorial and want to leave a tip?
PayPal - https://www.paypal.me/cgmasterscom
Bitcoin - bc1qh2cx8kz4wjj855cruk2sfwftl69vmmuau94w9v
Ethereum - 0xfb2D6886347434Eb2DACde22C6b96889dDAE6255
XRP - r4A4bxQ5wK8R1rEhecgvTy1wSXoCfoE42b
Dogecoin - DLDZ8fngi4KJkNnHQafV4vxDwaPzZ7e92W