When ever I run the game and the gameobject hits player 2 it keeps on spinning instead of going back to player 1. Please help.
void FixedUpdate () {
rb.AddForce(transform.up * (speed * Time.deltaTime));
}
void Hit()
{
if (hit == 2)
{
rand = Random.Range(1, 3);
if (rand == 1)
{
transform.Rotate(0, 0, threetoone);
spot = 1;
}
if (rand == 2)
{
transform.Rotate(0, 0, 180);
spot = 2;
}
}
}
private void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.name == “Player1”)
{
Debug.Log(“Hit1”);
hit = 1;
Hit();
}
if (coll.gameObject.name == “Player2”)
{
Debug.Log(“Hit2”);
hit = 2;
Hit();
}
}
You’re using rb.AddForce in FixedUpdate(), and calling transform.Rotate when a player collides.
Use this as a base and fix it up from your end!
void FixedUpdate () {
rb.AddForce(transform.up * (speed * Time.deltaTime));
}
void Hit ( ) {
if (hit == 2) {
rand = Random.Range(1, 3);
if (rand == 1)
rb.AddForce(transform.up * (new Vector3(0, 0, threetoone) * Time.deltaTime));
spot = 1;
} else if (rand == 2) {
rb.AddForce(transform.up * (new Vector3(0, 0, 180) * Time.deltaTime));
spot = 2;
}
}
}
private void OnCollisionEnter2D (Collision2D coll) {
if (coll.gameObject.name == "Player1") {
Debug.Log("Hit1");
hit = 1;
Hit();
}
if (coll.gameObject.name == "Player2") {
Debug.Log("Hit2");
hit = 2;
Hit();
}
}