- Home /
This question was
closed Jan 28, 2020 at 10:11 AM by
emmags9519.
Question by
emmags9519 · Jan 28, 2020 at 11:07 AM ·
c#animation3dmouseclickkeypress
Animation will only play once with mouse click and key press, i want it to play anytime these are pressed together.,animation will only play once, but wont play at all if another animation plays first.
Hi, I am having issues with my character not playing skip animation more than once. the skip will only play if it is triggered before anything else.
Here is my code:
void Update() { //declaring miffy's position as curPos Vector3 curPos = transform.position;
if (Input.GetMouseButtonDown(0))
{
//defining targetPos as the mouse click position
Vector3 targetPos = Input.mousePosition;
//outputting to console target position
Debug.Log("Mouse Position " + targetPos);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
//newPos is the position of where miffy is meant to go.
newPos = new Vector3(hit.point.x, 0.1199974f, hit.point.z);
//setting isMiffyMoving to true to carry out miffyMove() actions
isMiffyMoving = true;
}
//if statements to detmine miffy's direction when shes moving and output to console.
if (targetPos.x < curPos.x)
{
Debug.Log("Miffy is going left");
left = true;
isIdle = false;
}
else if (targetPos.x > curPos.x)
{
Debug.Log("Miffy is going right");
left = false;
isIdle = false;
}
if (targetPos.z < curPos.z)
{
Debug.Log("Miffy is going backward");
forward = false;
isIdle = false;
}
else if (targetPos.z > curPos.z)
{
Debug.Log("Miffy is going forward");
forward = true;
isIdle = false;
}
//setting isIdle to true is miffy is not moving
if (isIdle)
{
Debug.Log("Miffy is idle");
isIdle = true;
}
}
//if keypress S and mouseclick 0 skip = true and carry out MiffySkip() actions.
if (Input.GetMouseButtonDown(2) && Input.GetKeyDown(KeyCode.S))
{
//defining targetPos as the mouse click position
Vector3 targetPos = Input.mousePosition;
//outputting to console target position
Debug.Log("Mouse Position " + targetPos);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
//newPos is the position of where miffy is meant to go.
newPos = new Vector3(hit.point.x, 0.1199974f, hit.point.z);
//setting skip to true to carry out miffySkip() actions
skip = true;
}
//if statements to detmine miffy's direction when shes moving and output to console.
if (targetPos.x < curPos.x)
{
Debug.Log("Miffy is going left");
left = true;
isIdle = false;
}
else if (targetPos.x > curPos.x)
{
Debug.Log("Miffy is going right");
left = false;
isIdle = false;
}
if (targetPos.z < curPos.z)
{
Debug.Log("Miffy is going backward");
forward = false;
isIdle = false;
}
else if (targetPos.z > curPos.z)
{
Debug.Log("Miffy is going forward");
forward = true;
isIdle = false;
}
//setting isIdle to true is miffy is not moving
if (isIdle)
{
Debug.Log("Miffy is idle");
isIdle = true;
}
}
//if up arrow is pressed animation stored in Spin will play
if (Input.GetKeyDown(KeyCode.UpArrow))
{
//setting idle to false to ensure it will not execute while spin is executing.
isIdle = false;
//setting cheer to false to ensure it will not execute while spin is executing.
cheer = false;
//setting spin to true.
spin = true;
skip = false;
// playing animation for miffy to spin.
anim.Play("SpinJump", 0, 0f);
//testing if input is working by outputting text "working" to console.
Debug.Log("Working");
}
////if space has been pressed animation stored in Cheer with play
if (Input.GetKeyDown(KeyCode.Space))
{
//setting idle to false to ensure it will not execute while cheer is executing.
isIdle = false;
//setting spin to false to ensure it will not execute while cheer is executing.
spin = false;
//setting cheer to true.
cheer = true;
skip = false;
// playing animation for miffy to cheer.
anim.Play("Cheer", 0, 0f);
//testing if input is working by outputting text "working" to console.
Debug.Log("Working");
}
//if miffy is moving to a different position carry out MiffyMove() actions
if (isMiffyMoving == true)
{
MiffyMove();
skip = false;
}
//carry out MiffySkip() actions.
if (skip == true)
{
MiffySkip();
walk = false;
}
//if miffy is idle play idle animation
if (isIdle == true)
{
//animation play "Idle" from animator
anim.Play("Idle");
skip = false;
walk = false;
}
}
//rotates miffy towards new position
void MiffyRotate()
{
// setting cheer and spin to false while miffy is rotating
cheer = false;
spin = false;
//setting target rotation to mouse click direction
Quaternion targetRotation = Quaternion.LookRotation(newPos - transform.position);
//rotating miffy based on target rotation smoothly
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime * 10);
//other method to rotate miffy, jump rotate, not a smooth rotation.
//transform.LookAt(newPos);
//transform.rotation = Quaternion.LookRotation(newPos - transform.position);
}
//moves miffy to her destination,plays animation during walk, and calls MiffyRotate() to rotate miffy in direction of destination
void MiffyMove()
{
//setting cheer and spin to false while miffy is moving
cheer = false;
spin = false;
//Rotate miffy in direction of destination
MiffyRotate();
//move miffy to position clicked on screen
transform.position = Vector3.MoveTowards(transform.position, newPos, Time.deltaTime * speed);
//play walk animation while miffy is moving
anim.Play("Walk");
//if miffy's current position is the same as the new position after moving then miffy is not moving and miffy is idle
if (transform.position == newPos)
{
isMiffyMoving = false;
isIdle = true;
}
}
void MiffySkip()
{
// setting idle to false to ensure it will not execute while cheer is executing.
isIdle = false;
//setting spin to false to ensure it will not execute while cheer is executing.
spin = false;
//setting cheer to false.
cheer = false;
//setting walk to false
walk = false;
//setting skip to true
skip = true;
// setting isMiffyMoving to false
isMiffyMoving = false;
//Rotate miffy in direction of destination
MiffyRotate();
//move miffy to position clicked on screen
transform.position = Vector3.MoveTowards(transform.position, newPos, Time.deltaTime * speed);
// playing animation for miffy to skip while moving.
anim.Play("Skip");
//testing if input is working by outputting text "working" to console.
Debug.Log("Skip Working");
//if miffy's current position is the same as the new position after moving then miffy is not moving and miffy is idle
if (transform.position == newPos)
{
isIdle = true;
}
}
}
she will only play the skip and move to the destination once when the skip is triggered by "S"key and mouse click. after that the walk will play with mouse click, and with "S"key and mouse click. I have tried to also change the transition and animation settings about in the animator but nothing seems to be helping. I am new to unity! Thanks.
Comment