- Home /
Question by
FunkiestFak · Jul 17, 2020 at 02:39 AM ·
velocitymovingconstant
How to keep an object from speeding up or slowing down?
I'm making a pong game to learn the basics of how to make games. I'm almost done with the game but sometimes when the player hits the ball at a certain angle the ball speeds up or it slows down, how do I make it so that the ball is always going at a constant speed and it doesn't speed up or slow down?
here's the code for the ball.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallMovement : MonoBehaviour
{
private int playerScore;
private int enemyScore;
private int randomRotation;
[SerializeField] private float launchPower = 500;
private void Awake()
{
randomRotation = Random.Range(0, 361);
if (randomRotation == 90 ||
randomRotation == 180 ||
randomRotation == 270)
{
randomRotation += 10;
}
GetComponent<Rigidbody2D>().SetRotation(90);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody2D>().AddForce(transform.up * launchPower);
}
}
public void OnCollisionEnter2D(Collision2D collision)
{
EnemyGoal enemyGoal = collision.collider.GetComponent<EnemyGoal>();
PlayerGoal playerGoal = collision.collider.GetComponent<PlayerGoal>();
if (playerGoal != null)
{
enemyScore++;
}
if (enemyGoal != null)
{
playerScore++;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Rigidbody enemy Forcemode.VelocityChange 1 Answer
2D Sprite/object rotation changing despite telling it not to 0 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
How do I keep constant velocity on my rigidbody? 1 Answer
NullReferenceException: Object reference not set to an instance of an object CannonFire.Update 0 Answers