Check/Change velocity of an object
Hey Guys, I'm complete new to Unity and made an Arkanoid clone with an YT tutorial to get into Unity and try now some extra features and little fixes on myself to get more in touch with Unity and C#.
I got the problem that from time to time my ball move nearly horizontally (nearly 0 velocity on y-axis). If that happens i wanna give the ball a little push on the axis. I read many topics now and also found some information in the documentation but i absolutely dont get how i can check the velocity of the ball and change it if its under a certain value.
Thanks for anyone who can help me with that.
void Start()
{
_rigidbody = GetComponent<Rigidbody>();
_renderer = GetComponent<Renderer>();
Invoke("Launch", 0.5f);
}
void Launch()
{
_rigidbody.velocity = Vector3.down * _speed;
}
void FixedUpdate()
{
_rigidbody.velocity = _rigidbody.velocity.normalized * _speed;
_velocity = _rigidbody.velocity;
if (!_renderer.isVisible)
{
GameManager.Instance.Balls--;
Destroy(gameObject);
}
}
private void OnCollisionEnter(Collision collision)
{
AudioSource.PlayClipAtPoint(hitSound, new Vector3(0, 0, -50), 0.15f);
_rigidbody.velocity = Vector3.Reflect(_velocity, collision.contacts[0].normal);
}
Comment