How to make objects spawned from 2 players go in different directions.
So for my game, I have two players on opposite sides of the screen spawning robots to break each player's walls.
At the moment, I have it so when player 1 spawns a robot, it moves to the right. But at the moment, I can't figure out how to make the robots spawned by player 2 go to the left.
What I need to figure out is how can I tell the robot which player spawned it? In other words, how can I make the player script tell the robot script to change IsPlayer1/IsPlayer2 to true?
Here is part of my script for one of the robots when spawned.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BasicBorg_G : MonoBehaviour {
public float _speed;
public float movementSpeed = 5.0f;
bool MoveRight = false;
bool MoveLeft = false;
public bool IsPlayerOne = false;
public bool IsCPU = false;
public bool IsPlayerTwo = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (IsPlayerOne == true)
{
MoveRight = true;
if (MoveRight)
{
transform.Translate(Vector3.right * Time.deltaTime);
}
}
if (IsPlayerTwo == true)
{
MoveLeft = true;
if (MoveLeft)
{
transform.Translate(Vector3.left * Time.deltaTime);
}
}
if (transform.position.x > 10f)
{
Destroy(this.gameObject);
}
//keeps the robot from going offscren on the y axis
if (transform.position.y >= 5f)
{
transform.position = new Vector3(transform.position.x, 4f, 6f);
}
if (transform.position.y <= -5f)
{
transform.position = new Vector3(transform.position.x, -4f, 6f);
}
}
And here is part of my player script,
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour {
public float _speed;
public float movementSpeed = 5.0f;
[SerializeField]
private GameObject BasicBorg_SPrefab;
public bool IsPlayerOne = false;
public bool IsCPU = false;
public bool IsPlayerTwo = false;
[SerializeField]
private float SpawnRate = 5.5f;
private float CanSpawn = -1f;
public float SpawnNext;
[SerializeField]
public List<GameObject> robots;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (IsPlayerOne == true)
{
CalculateMovment();
SpawningRobot();
}
if (IsPlayerTwo == true)
{
CalculateMovment2();
SpawningRobot2();
}
if (transform.position.y >= 5f)
{
transform.position = new Vector3(transform.position.x, 4f, 6f);
}
if (transform.position.y <= -5f)
{
transform.position = new Vector3(transform.position.x, -4f, 6f);
}
}
void SpawningRobot()
{
if (Input.GetKeyDown(KeyCode.Space) && Time.time > CanSpawn)
{
CanSpawn = Time.time + SpawnRate;
Instantiate(ChooseRobot(), transform.position, Quaternion.identity);
}
}
void SpawningRobot2()
{
if (Input.GetKeyDown(KeyCode.KeypadEnter) && Time.time > CanSpawn)
{
CanSpawn = Time.time + SpawnRate;
Instantiate(ChooseRobot(), transform.position, Quaternion.identity);
}
}
public GameObject ChooseRobot()
{
int randomNumber = (int)Random.Range(0, robots.Count - 1);
return robots[randomNumber];
}