I need to modify this script to work for two different players in the same scene. It works for player 1 now. I need it to work for player 2 with different inputs. Please help and explain steps to implement within Unity. Thanks
Here is script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed = 5;
public LayerMask playerMask;
public float jumpVelocity = 20;
public bool canMoveInAir = true;
Transform myTrans, tagGround;
Rigidbody2D myBody;
public bool isGrounded = false;
float hInput;
AnimatorController myAnim;
void Start ()
{
myBody = this.GetComponent<Rigidbody2D>();
myTrans = this.transform;
tagGround = GameObject.Find (this.name + "/tag_ground").transform;
myAnim = AnimatorController.instance;
}
void FixedUpdate ()
{
isGrounded = Physics2D.Linecast(myTrans.position, tagGround.position, playerMask);
myAnim.UpdateIsGrounded (isGrounded);
#if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT || UNITY_EDITOR
hInput = Input.GetAxisRaw ("Horizontal");
myAnim.UpdateSpeed (hInput);
if (Input.GetButtonDown ("Jump"))
Jump();
#endif
Move (hInput);
}
public void Move (float horizontalInput)
{
if (!canMoveInAir && !isGrounded)
return;
Vector2 moveVel = myBody.velocity;
moveVel.x = horizontalInput * speed;
myBody.velocity = moveVel;
}
public void Jump()
{
if(isGrounded)
myBody.velocity += jumpVelocity * Vector2.up;
}
public void StartMoving(float horizontalInput)
{
hInput = horizontalInput;
myAnim.UpdateSpeed (horizontalInput);
}
}
Someone suggested this (but I do not know how to implement it.)
using UnityEngine;
using System.Collections;
public class Player2Script : MonoBehaviour {
public float speed = 5;
public LayerMask playerMask;
public float jumpVelocity = 20;
public bool canMoveInAir = true;
Transform myTrans, tagGround;
Rigidbody2D myBody;
public bool isGrounded = false;
float hInput;
AnimatorController myAnim;
public int PlayerId = 0;
void Start ()
{
myBody = this.GetComponent<Rigidbody2D>();
myTrans = this.transform;
tagGround = GameObject.Find (this.name + "/tag_ground").transform;
myAnim = AnimatorController.instance;
}
void FixedUpdate ()
{
isGrounded = Physics2D.Linecast(myTrans.position, tagGround.position, playerMask);
myAnim.UpdateIsGrounded (isGrounded);
#if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT || UNITY_EDITOR
if(PlayerId == 0)
{
hInput = Input.GetAxisRaw ("Horizontal_Player0");
myAnim.UpdateSpeed (hInput);
if (Input.GetButtonDown ("Jump_Player0"))
Jump();
}
else if(PlayerId == 1)
{
hInput = Input.GetAxisRaw ("Horizontal_Player1");
myAnim.UpdateSpeed (hInput);
if (Input.GetButtonDown ("Jump_Player1"))
Jump();
}
#endif
Move (hInput);
}
public void Move (float horizontalInput)
{
if (!canMoveInAir && !isGrounded)
return;
Vector2 moveVel = myBody.velocity;
moveVel.x = horizontalInput * speed;
myBody.velocity = moveVel;
}
public void Jump()
{
if(isGrounded)
myBody.velocity += jumpVelocity * Vector2.up;
}
public void StartMoving(float horizontalInput)
{
hInput = horizontalInput;
myAnim.UpdateSpeed (horizontalInput);
}