- Home /
Swtich weapons problem (attach to bone)
I made a sword that need to be pull when I switching weapons. I used for that a code that attach the sword to the bone in the hand when a variable in the character's code is true. The problem is that the sword is always attached to the body's bone and never change it (the sword should been attach to the body's bone when I use another weapon, when I use the sword it should been attach to the hand's bone). Is there any way to make the code works or there is a better way to pull out the swrod?
This is my code:
var target : Transform;
function Awake()
{
if (BobC.putSword)
{
target = GameObject.FindWithTag("Hand").transform;
transform.parent = target.transform;
}
if (!BobC.putSword)
{
target = GameObject.FindWithTag("Body").transform;
transform.parent = target.transform;
}
}
The code should work when in the withing animation. The sword have no rigidbody and no collider.
Sorry for poor english.
Awake only runs once. Does putSword always start false? To see if the Hand code works, try running just the two lines target=..."Hand" ...
. If she starts with it in her hand, the problem isn't there.
Ins$$anonymous$$d of "if (!BobC.putSword)" I would just say "else"
You could look into "animation event" which I think will execute things based on how far along an animation is.
Or just have a whole bone just for the sword, that way when you draw it the sword will never be actually attached to the hand or body at all. This will even make it possible to do more fancy moves like maybe throw sword in the air and catch it with another hand
Answer by nhftk12 · Jun 01, 2011 at 06:24 AM
I solved the problem by myself (thnx to Owen Reynolds who sade "Awake only runs once"). I made 2 swords: one attached to the hand and other to the body. With the fact that Awake runs only once I edited the code a bit and attached the code to every part of both swords and every sword attached to another bone from the beginning. in the code I just enabled and disable the renderer every time the variable is true of false (when the sword is been pull out the renderer is visible and when it pull in it not visible).
This is the body's sword code:
var target : Transform;
function Awake ()
{
target = GameObject.FindWithTag("Body").transform;
transform.parent = target.transform;
}
function Update ()
{
if (BobC.putSword)
{
renderer.enabled = false;
}
else
{
renderer.enabled = true;
}
}
The hand's sword have a similar code to the body's sword but it attached to the hand and not the body and the renderer is visible when the other sword is not visible so it seems like I pull the sword and the sword is not just disappear.
Sorry for poor english.
Your answer
Follow this Question
Related Questions
Programming a Sword Swing Animation and Swapping weapons 1 Answer
Coroutine / Switch case 1 Answer
switch > door animation 0 Answers
Is there any hotkey to swich to the next/previous frame in the animation windows? 2 Answers
How do I have my animated character have a weapon switching system? 0 Answers