- Home /
How to disable "GameObject" Then Enable After Animation.
I'm adding a melee script to my FPS. so if i pres V it stabs them.
The Animation Will Be attached to a gameobject named "Quickstab" I want the mesh or mesh renderer to be invisible "Disabled" untill i press "V" and then once the animation is finished i want the object to then be disabled again. Does someone know how to achieve this? Prefer in Javascript since i dont know cs. thanks!
Answer by theundergod · Feb 24, 2013 at 03:08 PM
One way to work through something like this (in any programming challenge actually) is think about every step that needs to occur:
You need to have a GameObject assigned for the QuickStab
You need to initially disable the renderers on it
Wait for the input of the "V" key
Once pressed enable the renderers
Play the animation
Check to see if the animation is complete
Disable the renderers
So one way to do it would be to create a script specifically for this object "QuickStab" object and place it on the object:
Renderer[] renderers;
string stabAnimation = "StabAnimation";
void Start()
{
//get the renderers and store for later use
renderers = GetComponentsInChildren<Renderer>();
//turn off renderers initially
foreach(Renderer r in renderers)
{
r.enabled = false;
}
}
void Update()
{
//if V is pressed and we are not playing the animation
if(Input.GetKeyDown(KeyCode.V) && !animation.IsPlaying(stabAnimation))
{
//turn on renderers and start playing the animation
foreach(Renderer r in renderers)
{
r.enabled = true;
}
//play the animation
animation.Play(stabAnimation);
}
//if we are playing the animation
else if (animation[stabAnimation].time <= animation[stabAnimation].length)
{
//if we are playing and not at the end of the animation
if(animation.IsPlaying(stabAnimation))
{
//do something like check for raycast hits
}
//we are at the end of the animation
else
{
//ensure animation stops
animation.Stop(stabAnimation);
//disable renderers
foreach(Renderer r in renderers)
{
r.enabled = false;
}
}
}
}
This is just one of many ways to accomplish something like this. Sorry it's in C#. I'm a C# guy.
Here's my attempt to put it in JavaScript:
var renderers : Renderer[];
var stabAnimation : string = "StabAnimation";
function Start()
{
//get the renderers and store for later use
renderers = GetComponentsInChildren(Renderer);
//turn off renderers initially
for(var x : int = 0; x < renderers.length; x++)
{
r.enabled = false;
}
}
function Update()
{
//if V is pressed and we are not playing the animation
if(Input.GetKeyDown(KeyCode.V) && !animation.IsPlaying(stabAnimation))
{
//turn on renderers and start playing the animation
for(var x : int = 0; x < renderers.length; x++)
{
r.enabled = true;
}
//play the animation
animation.Play(stabAnimation);
}
//if we are playing the animation
else if (animation[stabAnimation].time <= animation[stabAnimation].length)
{
//if we are playing and not at the end of the animation
if(animation.IsPlaying(stabAnimation))
{
//do something like check for raycast hits
}
//we are at the end of the animation
else
{
//ensure animation stops
animation.Stop(stabAnimation);
//disable renderers
for(var x : int = 0; x < renderers.length; x++)
{
r.enabled = false;
}
}
}
}
Firstly Thank you for taking the time to reply in such detail really appreciate it. C# Script... 4 errors. line 1,2,4,16
A namespace can only contain types and namespace declarations
and the JS Version Line 2 var stabAnimation : string = "StabAnimation"; unknown format String
Not too sure about the C# since i have no idea about that code..
As far as the C# code is concerned it will only work if you put the above code in a C# script within the class declaration such as
using UnityEngine;
using System.Collections;
public class StabObject : $$anonymous$$onoBehaviour
{
//Paste the code here
}
For the Javascript I'm not sure. Does javascript accept double quotes (") or do you have to use single quotes (')?
C# Isnt Getting Those Errors Now.. But There getting this.
if(Input.GetButtonDown($$anonymous$$eyCode.V) && !animation.IsPlaying(stabAnimation))
Argument #1' cannot convert
UnityEngine.$$anonymous$$eyCode' expression to type string' The best overloaded method match for
UnityEngine.Input.GetButtonDown(string)' has some invalid arguments
same line.
JS uses both.. But the error is i dont think javascript uses "String" The name 'string' does not denote a valid type ('not found'). Did you mean 'Boo.Lang.Compiler.IO.StringInput'?
Oh one more thing with the Javascript: do you even declare string types in Javascript? Since it's loosely typed maybe it should just be:
var stabAnimation = "StabAnimation";
Like I said I'm a C# guy so sorry I'm not too much help with the Javascript.
Oops not GetButtonDown shoudl be Get$$anonymous$$eyDown. I will update the code to reflect the correction.
Your answer
Follow this Question
Related Questions
How to display GUI in sequence in trigger 3 Answers
change water color through script when an action occurs 3 Answers
Convert this line of javascript to C# (easy) 1 Answer
oescape script name? 1 Answer
Is it the same javascript 1 Answer