- Home /
Raycast play animation1 and then animation2
Hi,i made simple script,it plays animation when raycast hits cube. So problem is,what i want to do is : When i press "e" button cube play "Clip1" and WHEN i press again button "e" cube play animation "Clip2". Everything seems okey,but its not. Clip1 plays perfect but "Clip2" not. I done something wrong i know,can anyone help me?
var Cube : GameObject;
var Clip1 : AnimationClip;
var Clip2 : AnimationClip;
function Start()
{
Cube = GameObject.Find("Cube.This");
}
function Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit, 100.0F))
{
if(hit.transform.tag == "Cube")
{
print("First");
Cube.animation.Play("Clip1");
} else {
print("Second");
Cube.animation.Play("Clip2");
}
}
}
}
Something is wrong in else function.
Curious. Despite having no identation at all, your code looks solid. Is everything else in your scene set up correctly? is Clip2 working properly? (you can quickly check that by swapping clip 1 for clip2 on this code)
Answer by Tomer-Barkan · Nov 24, 2013 at 05:17 PM
Your code is built differently than what you explain. It's built so that if you press 'e' (`if (Input.GetKeyDown(KeyCode.E))`), it checks whether or not the player is in front of the object tagged "Cube" (`if(hit.transform.tag == "Cube")`), and then plays clip1. If the player is in front of ANOTHER object, not "Cube" (`else`, line 24) - then play clip2.
What you want is a boolean that flips between clip1 and clip2 every time you press 'e':
var isClip1 : bool;
function Start() {
Cube = GameObject.Find("Cube.This");
isClip1 = true;
}
function Update() {
if (Input.GetKeyDown(KeyCode.E)) { // check that 'e' is pressed
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit, 100.0F) && hit.transform.tag == "Cube") { // check that raycast hit "Cube"
if (isClip1) {
Cube.animation.Play("Clip1"); // if bool play clip1
} else {
Cube.animation.Play("Clip2"); // if not bool play clip2
}
isClip1 = !isClip1; // reverse bool so next time the other clip plays
}
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Animation Script Problem. 1 Answer
Field of view scripted animation curve 1 Answer
How to run an animation mouse click. 2 Answers
Player is running just few seconds 0 Answers