- Home /
Why does the canvas image, go, show up as soon as I press Mouse1?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Scope : MonoBehaviour {
public Animation scope;
public GameObject go;
// Use this for initialization
void Start()
{
scope = gameObject.GetComponent<Animation>();
go.SetActive(false);
}
// Update is called once per frame
void Update()
{
foreach (AnimationState state in scope)
{
if (Input.GetMouseButtonDown(1))
{
state.speed = 2;
scope.Play();
}
if (Input.GetMouseButtonUp(1))
{
state.speed = -2;
}
if (Input.GetMouseButtonUp(1) && state.time == 0f)
{
state.speed = -2;
state.time = 0.49f;
scope.Play();
}
if (Input.GetMouseButton(1) && state.time == 0f)
{
go.SetActive(true);
}
else
{
go.SetActive(false);
}
}
}
}
Answer by Owen_Smart · Jul 02, 2018 at 12:52 AM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Scope : MonoBehaviour {
public Animation scope;
public GameObject go;
// Use this for initialization
void Start()
{
scope = gameObject.GetComponent<Animation>();
go.SetActive(false);
}
// Update is called once per frame
void Update()
{
foreach (AnimationState state in scope)
{
if (Input.GetMouseButtonDown(1))
{
state.speed = 2;
scope.Play();
}
if (Input.GetMouseButtonUp(1))
{
state.speed = -2;
}
if (Input.GetMouseButtonUp(1) && state.time == 0f)
{
go.SetActive(false);
state.speed = -2;
state.time = 0.49f;
scope.Play();
}
if (Input.GetMouseButton(1) && state.time == 0f)
{
StartCoroutine("OnScoped");
}
if (Input.GetMouseButton(1) && state.time != 0f)
{
go.SetActive(false);
}
}
}
IEnumerator OnScoped()
{
yield return new WaitForEndOfFrame();
go.SetActive(true);
}
}
Since the animation wrap mode was set to play once, once the animation ends, state.time = 0f. Because,of that, I set it so that if the mouse click is being held down, and state.time = 0f, to enable the canvas image (go). But, it also recognizes me pressing the button as 0f. So by setting a coroutine and waiting till the end of frame, it tried to enable go after state.time = 0f, but it can't.
Your answer
Follow this Question
Related Questions
Sniper scope zoom variances 0 Answers
Sniper Zooming 2 Answers
Sniper zoom 1 Answer
How to make Sniper Scope with Range pointer Digit as Sniper 3d assassian Game 1 Answer
Sniper scope zoom if crosshair is not in the middle of the screen 2 Answers