- Home /
Question by
ultranova0520 · Apr 28 at 07:53 AM ·
unity 2dresetthrow
Resetting a variable
I am making a throwing script where the player throws a ball. But I want the initial velocity to go down to 0 after the player throws is everytime. How do I make it work? Here is my code. Thanks in advance :)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Throw : MonoBehaviour { [SerializeField] float InitialVelocity; [SerializeField] float Angle; [SerializeField] Transform _FirePoint; private float angle; private float v;//initial velocity private bool IsThrown;
private void Update()
{
if (Input.GetKey(KeyCode.Space))
{
//this.angle = Angle * 3;
//v0 = InitialVelocity * -0.5f;
//Debug.Log(angle);
Debug.Log(v);
Increment();
StopAllCoroutines();
StartCoroutine(Coroutine_Movement());
IsThrown = true;
}
}
IEnumerator Coroutine_Movement()
{
float t = 0; //time
while (t < 100)
{
float x = v * t * Mathf.Cos(angle);
float y = v * t * Mathf.Sin(angle) - (1f / 2f) * -Physics.gravity.y * Mathf.Pow(t, 2);
transform.position = _FirePoint.position + new Vector3(x, y + 1f, 0);
t += Time.deltaTime;
yield return null;
//v0 = 0;
}
}
private void Increment()
{
if(Input.GetKey(KeyCode.Space))
{
//angle = Angle * Time.deltaTime;
v += (InitialVelocity + 0.2f) * Time.deltaTime;
}
}
}
Comment
Your answer
