Using a class to control two "player" objects
Hi all! I'm working on some homework and i'm looking for a little guidance. Anyone that's been through the forums and/or YouTube has seen the "roll-a-ball" tutorial. For my intro to c# class, i'm having to create my own modified version of this game. I already have one version with a single player, 3 enemies, and a win condition. That version works. My task in this version is to add a second player that moves using separate inputs, and i have to create classes for my two players and enemies. What is the best way to approach this? I want to only focus on the player at the moment. Once i figure out what i need to do i can apply it to the enemies later.
I have several triggers within my current playerController script that i need to function. Can that be part of the class? It will need to be part of both players interaction with the environment.
Since both of my players will be receiving different inputs to govern their movements, should i create a simple script for both that only handles the movement of the players and let the playerController/class script handle everything else? My assignment does specifically say "these classes should have the ability to move in the world". How do i designate different inputs for two objects of the same class?
Also, can input manager be used to solve some of my questions? I've been looking through it but all the settings seem to be general settings that apply to all objects, not just a single object.
I hope you guys can point me in the right direction.
This is my movement script for my two "players". using UnityEngine; using System.Collections;
public class player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
Rigidbody rb;
float moveHorizontal;
float moveVertical;
public float speed;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
if (this.gameObject.tag == "Player1")
{
moveHorizontal = Input.GetAxis ("Horizontal");
moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
if (this.gameObject.tag == "Player2")
{
moveHorizontal = Input.GetAxis ("P2Horizontal");
moveVertical = Input.GetAxis ("P2Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
} What i need to know is how can I create a class that governs my two objects using this code? When I get rid of the the "mono behavior" inheritance to create the class the script stops working. Also, should i move the "moveHorizontal = Input.GetAxis ("Horizontal");" variable assignment to the object itself and pass the "moveHorizontal" variable into the class? I've tried it previously without success.
okay.... so i can't add anything to an object in unity that isn't derived from $$anonymous$$ono Behavior. Does that mean that i will not be able to have my movement script as part of the class?
Answer by Ali-hatem · Oct 28, 2015 at 11:13 AM
//tag each gameobject in hierarchy & test its tag in the same script:
if (this.gameobject.tag == "player")
{
//do something
}
if (this.gameobject.tag == "enemy")
{
//do something
}
Thank You Ali! That helped with one of my problems. I can now move the players independently with different key bindings.
@mcabral89 : i am sorry i didn't read the class example its like my answer already in your script . but i still not understand what is the problem if you just attach this script to all the game objects it will work fine and you don't need a reference to the game object just use rigidbody.AddForce and delete rb.