prevent animation loop.
So I have problem with script, I just need to play zoom in animation when right mouse button is pressed, but it starts looping, here is my code:
using UnityEngine;
using System.Collections;
public class testAnimScript : MonoBehaviour {
//Holder
public Transform WalkAnimationHolder;
public bool running = false;
public float interval = 4.5f;
public bool zoomed = false;
public WalkingState walkingState = WalkingState.Idle;
public void FixedUpdate()
{
AnimationController();
SpeedController();
}
public void SpeedController()
{
//When we press right mouse button....
if (Input.GetMouseButtonDown(1) && !running && !zoomed)
{
zoomed = true;
walkingState = WalkingState.Zoom;
}
if (Input.GetMouseButtonUp(1))
{
zoomed = false;
}
//other...
if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && !running && !zoomed)
{
if (Input.GetButton("Run"))
{
walkingState = WalkingState.Running;
}
else
{
walkingState = WalkingState.Walking;
}
}
else
{
if (!running && !zoomed)
{
walkingState = WalkingState.Idle;
}
}
}
public void AnimationController()
{
if(walkingState == WalkingState.Zoom)
{
// As I think here is my problem, basically to prevent animation loop, i do something like that: if it's time is more than 0.15 secs then, make it 0.15 so it will play once.
if (WalkAnimationHolder.GetComponent<Animation>()["ak47zoom"].time >= 0.15f)
{
WalkAnimationHolder.GetComponent<Animation>()["ak47zoom"].time = 0.15f;
Debug.Log(WalkAnimationHolder.GetComponent<Animation>()["ak47zoom"].time);
}
WalkAnimationHolder.GetComponent<Animation>().CrossFade("ak47zoom", 0.2f);
}
if (!zoomed)
{
if (walkingState == WalkingState.Running)
{
WalkAnimationHolder.GetComponent<Animation>()["ak47run"].speed = 0.5f;
WalkAnimationHolder.GetComponent<Animation>().CrossFade("ak47run", 0.2f);
}
else if (walkingState == WalkingState.Walking)
{
WalkAnimationHolder.GetComponent<Animation>()["ak47walk"].speed = 0.5f;
WalkAnimationHolder.GetComponent<Animation>().CrossFade("ak47walk", 0.2f);
}
else
{
WalkAnimationHolder.GetComponent<Animation>().CrossFade("ak47breath", 0.2f);
}
if (walkingState == WalkingState.Reload)
{
WalkAnimationHolder.GetComponent<Animation>().CrossFade("ak47reload", 0.2f);
}
}
}
public enum WalkingState
{
Idle,
Walking,
Running,
Zoom,
Shoot,
Reload
}
void reload()
{
if (!running)
{
StartCoroutine(reloadGun());
}
}
private IEnumerator reloadGun()
{
running = true;
walkingState = WalkingState.Reload;
yield return new WaitForSeconds(interval);
running = false;
}
}
here is link to my video btw: youtube video
any help, comment, etc will be helpful! if code isn't readable just comment, I will try to make it smaller. :)
thanks.
-Nick
FixedUpdate() is used for physics, better not try to use it for getting input, you will miss some. You should use Update() insted.
You can use switch operator ins$$anonymous$$d of that amount of if-s.
Also I'd suggest not to do GetComponent every frame cause this approach can drop your FPS significantly once there will be some number of scripts doing it.
Looks like it loops because once you do walkingState = WalkingState.Zoom;
, you never change it back to any other state.
Hi! thanks for your comments, yes I moved getmousebutton into the update, and I actually change walkingstate, in this part: if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && !running && !zoomed) { if (Input.GetButton("Run")) %|-1184559434_3|% walkingState = WalkingState.Running; } else %|-1421267895_7|% %|363040906_8|% %|-1230046081_9|% }
``but I don't think I understand what you mean.
Your answer
Follow this Question
Related Questions
How do I delete objects in an animation in a certain order? 1 Answer
Can not select loop time or any other stuff 0 Answers
Animation Panel: how to turn an existing loop into just a standing sprite and then implement it 0 Answers
When walking animation loops in timeline, it resets the player´s position back to the start 0 Answers
How to get perfect position animation with root motion? 1 Answer