Stop animation OnTriggerExit - animation

I am using collider to play my animation onTriggerEnter and want to stop the animation onTriggerExit and play again onTriggerEnter and so on.
Here's the onTriggerEnter script:
var chestSound : AudioClip;
var treasureChest : GameObject;
function OnTriggerEnter (col : Collider) {
if(col.gameObject.tag == "Player") {
AudioSource.PlayClipAtPoint(chestSound, transform.position);
treasureChest.animation.Play();
Destroy(gameObject);
}
}

Well, I would write
function OnTriggerExit (col : Collider) {
if(col.gameObject.tag == "Player")
treasureChest.animation.Stop();
}
but it seems that maybe your use of Destroy(gameObject) could affecting the game. you are destroying your own player object as he triggers the other object, how could he go through OnTriggerExit?

Related

Unity freezes because of script, but i don't know whats wrong with said script

Bear with me, I'm pretty new to unity.
As the title suggests, the game engine freezes when this script is attached to the main camera.
public class leftright : MonoBehaviour {
public float boundaries = 3f;
void Update () {
while (Input.GetAxis("Mouse X") < boundaries && Input.GetAxis ("Mouse X") > -boundaries) {
this.transform.Rotate(0, Input.GetAxis("Mouse X"), 0);
}
}
}
I don't think this script makes an infinite loop, and I can't detect any problem with it.
the log text is here, and the project is here
While(true) {
//do stuff
}
The conditional you're using in your while statement cannot (and will not) ever change from true to false based on the contents of the loop, therefor it will run forever.
Update() is already a loop, treat it like one.
void Update () {
if(Input.GetAxis("Mouse X") < boundaries && Input.GetAxis ("Mouse X") > -boundaries) {
this.transform.Rotate(0, Input.GetAxis("Mouse X"), 0);
}
}

Algorithm about playing animations of multiple objects

I'm currently working on a project that instantiates 3d models on my scene and allows the user to play their animation on button click. However, in my current code, it only plays the animation of the first object it detects even tho there are other objects around. I want to play the animation of all objects on the scene. This is part of my current code. This is the method that will fire up once the button is clicked.
public void PlayAnimation()
{
if (GameObject.FindGameObjectWithTag ("3DObject).name.Contains ("Spidey"))
{
int spideyAnimation = Random.Range(0, spideyAnimations.Length);
GameObject.FindGameObjectWithTag ("3DObject").GetComponent<Animation> ().Play (spideyAnimations[spideyAnimation]);
}
if (GameObject.FindGameObjectWithTag ("3DObject").name.Contains("Dino"))
{
int dinoAnimation = Random.Range(0, dinoAnimations.Length);
GameObject.FindGameObjectWithTag ("3DObject").GetComponent<Animation> ().Play (dinoAnimations[dinoAnimation]);
}
}
You can try something like this:
var arr = GameObject.FindGameObjectsWithTag ("3DObject");
foreach (var o in arr) {
if (o.name.Contains ("Spidey")) {
// play spideyAnimation
} else if (o.name.Contains ("Dino")) {
// play dino animation
}
}
Your current code will only find the first object, because you're calling GameObject.FindGameObjectWithTag and not GameObject.FindGameObjectsWithTag
UnityDocs - GameObject.FindGameObjectsWithTag

Tracking frames and/or time during an animation

Can I call a function(one that will make another object visible/invisible) on a specific animation frame or time? I would like to have arrows describe the movement of the animation at certain times during the animation. While I can just make them visible when I start the animation and make them invisible when the animation stops, I would like to specify ranges inside the animation to do this
playPatientAnim: function (anim, callback) {
var pending = 1;
var me = this;
var finish = callback ? function () {
if (pending && !--pending) {
callback.call(me, anim);
}
} : null;
me.currentPatient.skinned.forEach(function (mesh) {
mesh.animations.forEach(function(anim){
anim.stop();
});
});
me.currentPatient.skinned.forEach(function (mesh) {
var animation = mesh.animations[anim];
animation.stop();
if (animation) {
pending++;
animation.onComplete = finish;
animation.play();
}
});
if (finish) {
finish();
}
}
You can make a mesh visible or invisible ( mesh.visible = false; //or true ). To change visibility at certain time you could use timestamp:
new Date().getTime() and calculate how you want to do the sequence of your animation.

I have two 3dtext, one plays the animation, the other reverses it. After one go, the animation wont play anymore, why?

I have a 3dtext named Play, which when clicked will play the animation; the other one is named Back, which reverses the animation. Problem is after I Played and Backed it, the animation wont play anymore when i clicked Play.
The animation named redsubmenu is in legacy and clamp forever wrap mode.
public class PlayButtonScript : MonoBehaviour {
//public static PlayButtonScript pbs;
public GameObject redsubmenu;
void Update(){
#if UNITY_EDITOR
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Input.GetMouseButtonDown(0)&&Physics.Raycast(ray,out hit)){
if(hit.collider.name == "Play"){
redsubmenu.animation.Play();
}
}
#endif
}
}
public class BackButtonScript : MonoBehaviour {
// Update is called once per frame
void Update () {
#if UNITY_EDITOR
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Input.GetMouseButtonDown(0)&&Physics.Raycast(ray, out hit)){
if(hit.collider.name == "Back"){
transform.parent.animation["redsubmenu"].speed = -1;
transform.parent.animation.Play("redsubmenu");
}
}
#endif
}
}
It appears that you never reset the speed of the animation back to 1. When you click play the first time the speed is initially 1, so it works fine. However, when you back you set the speed to -1 and it is never set to any other value.
Try using:
if (hit.collider.name == "Play") {
transform.parent.animation["redsubmenu"].speed = 1;
redsubmenu.animation.Play();
}
in your play button script.
You might also be able to make use of Animation.Rewind.
http://docs.unity3d.com/ScriptReference/Animation.Rewind.html
Just to be more specific, i edited my playbuttonscript as shown below:
if(Input.GetMouseButtonDown(0)&&Physics.Raycast(ray,out hit)){
if(hit.collider.name == "Play"){
if(redsubmenu.animation["redsubmenu"].speed == -1){
redsubmenu.animation["redsubmenu"].speed = 1;
} else {
redsubmenu.animation.Play();
}
}
}
in my back button, i delete the transform.parent.animation.Play, no need for that.

Java ME. How to animate Sprite

I am having trouble with animating my sprite in Java ME.
if ((k & FIRE_PRESSED) != 0) {
spriteActive = true;
boxer.nextFrame();
if (boxer.getFrame() == boxer.getFrameSequenceLength() - 6) {
spriteActive = false;
}
}
}
// TO re-start a game...
public void update() {
if(boxer.getRawFrameCount() == 5 && spriteActive == false){
boxer.setFrame(0);
}
}
When the enter key is pressed, spriteActive is set true but only changes the frame by one. I intend to have it animating the entire sequence but it is not doing that. Just animates 1 frame at a time with every press.
Does anyone have any ideas/advice of how I should approach this?
Thanks for the time and help!
Do you have any code that checks whether spriteActive is true and then sets the next frame?
I am no expert on Java but I would suspect you'd need to implement something like this:
if (spriteActive == true)
{
boxer.nextFrame();
}

Resources