Problems disabling an AI and giving control to the Player
Hello,
I'm trying to make a Pong clone and I wanted to make it so that the player chooses whether the game will be singleplayer (with an AI on the other side) or multiplayer (another player controlling another racket) but I'm having problems disabling the AI and giving control to the second player.
The game has 2 scenes, one is the Main Menu and the other one is the game itself, on the Main Menu the player chooses if the game is going to be singleplayer or multiplayer by clicking a button and then the game scene loads, here is the code of the script to load the game scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class StartGameScript : MonoBehaviour {
public static bool singlePlayer;
public void LoadGameScene()
{
singlePlayer = true;
SceneManager.LoadScene ("Pong");
}
}
And here is the code of the right racket (the one with the AI):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AIScript : MonoBehaviour {
GameObject Ball;
public float speed = 30f;
public float lerpSpeed = 1f;
private Rigidbody2D rigidBody;
public string axis = "Vertical2";
void Start ()
{
rigidBody = GetComponent<Rigidbody2D> ();
}
void FixedUpdate ()
{
if (StartGameScript.singlePlayer == true)
{
Ball = GameObject.FindGameObjectWithTag ("Ball");
if (Ball.transform.position.y > transform.position.y)
{
if (rigidBody.velocity.y < 0)
rigidBody.velocity = Vector2.zero;
rigidBody.velocity = Vector2.Lerp (rigidBody.velocity, Vector2.up * speed, lerpSpeed * Time.deltaTime);
} else if (Ball.transform.position.y < transform.position.y)
{
if (rigidBody.velocity.y > 0)
rigidBody.velocity = Vector2.zero;
rigidBody.velocity = Vector2.Lerp (rigidBody.velocity, Vector2.down * speed, lerpSpeed * Time.deltaTime);
}
else
{
rigidBody.velocity = Vector2.Lerp (rigidBody.velocity, Vector2.zero * speed, lerpSpeed * Time.deltaTime);
}
}
else
{
if (StartGameScript.singlePlayer != true)
{
float v = Input.GetAxisRaw (axis);
rigidBody.velocity = new Vector2 (0, v) * speed;
}
}
}
}
My problem is no matter what the value of the singlePlayer bool is, the game always uses the AI and doesn't give control to the second player. Sorry if I made any grammar or spelling mistakes, English is not my native language.
Your answer
Follow this Question
Related Questions
How to make a thrown object land on a certain point e.g a thrown spear landing on its tip 0 Answers
Jitter when moving around a circular path 0 Answers
Is there a way I can make one script for all my buttons in my game? 1 Answer
What is the most effective way to structure Card Effects in a Single Player game? 1 Answer
How to make a thrown object land on a certain point e.g a thrown spear landing on its tip 1 Answer