- Home /
Sword Mesh renderer script
hey guys. i have a character model that i made in blender and imported to Unity. it has a sword and an attack animation that is part of the armature that i want both in the character's hand and put away while not in combat. I spent all day in Blender, but i couldn't find a way to properly do that. So, i just put the sword (which i imported seperately) as a child object of the armature in Unity.
i want to have two swords on the model, one on his side and one in his hand. When in combat, i want the mesh renderer of the sword in the hand on. if its not in combat, i want the mesh renderer of the sword on the side on. do you think there's a c# script or JavaScript for this? Thanks!
Answer by whydoidoit · Jun 16, 2012 at 09:29 PM
Yeah no problem :)
JS:
var swordBySide : MeshRenderer;
var swordInHand : MeshRenderer;
function EnterCombatMode() {
swordBySide.enabled = false;
swordInHand.enabled = true;
}
function ExitCombatMode() {
swordBySide.enabled = true;
swordInHand.enabled = false;
}
Attach this to you character and then drag and drop the gameobjects of the two swords on the inspector and call these functions when entering and exiting combat mode by doing:
SendMessage("EnterCombatMode");
from another script on the same game object.
the mesh or the entire object? its only letting me drag and drop meshes.
It will accept you dragging and dropping the thing that has a mesh renderer attached to it. That should be the mesh of the sword and it's associated game object.
thanks. another question, how can i have this work with the attack animation through animation.Play? here's the script im working on
using UnityEngine; using System.Collections;
public class AttackAnimation : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
animation.wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
}
// Update is called once per frame
void Update () {
if($$anonymous$$athf.Abs(Input.GetAxis("Attack")) > 0)
animation.Play("attack");
Send$$anonymous$$essage("EnterCombat$$anonymous$$ode");
animation.CrossFade("attack");
Send$$anonymous$$essage("ExitCombat$$anonymous$$ode");
}
}
this is c# because i tried java but it didnt work with the animation, and that got fixed in c#, but the mesh rendering of the swords isnt working correctly. i might be able to figure it out at some point, but do u have any suggestions? anything in c# would be great.
What exactly isn't working? BTW you should just use animation.CrossFade("attack") rather than that and Play.
well, im not exactly sure since i messed with the mesh renderers, but the mesh of one of the swords is still on, even though i have the codes you gave me about Send$$anonymous$$essage in the script. i also add some mesh components to the game objects of the swords. do u think u can help me on this? BTW i changed the animation.Play to CrossFade.
Your answer
Follow this Question
Related Questions
Why isn't this mesh not appearing properly? 0 Answers
Mesh Problems 1 Answer
Changing two different objects renderer colour 1 Answer