using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private Rigidbody2D rb;
public float velPower;
public float decceleration;
public float acceleration;
public float moveSpeed;
public float moveInput;
public float jumpForce;
public float airDec;
public float groundDec;
private bool isFacingRight = true;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
void Update()
{
moveInput = Input.GetAxisRaw("Horizontal 1");
if (Input.GetButtonDown("Vertical 1") && IsGrounded())
{
Jump();
}
Flip();
if (IsGrounded() == false )
{
decceleration = airDec;
}
if (IsGrounded() == true)
{
decceleration = groundDec;
}
}
// Update is called once per frame
void FixedUpdate()
{
float targetSpeed = moveInput * moveSpeed;
float speedDif = targetSpeed - rb.velocity.x;
float accelRate = (Mathf.Abs(targetSpeed) >0.01) ? acceleration : decceleration;
float movement = Mathf.Pow(Mathf.Abs(speedDif) * accelRate, velPower) * Mathf.Sign(speedDif);
rb.AddForce(movement * Vector2.right);
}
private void Flip()
{
if (isFacingRight && moveInput < 0f || !isFacingRight && moveInput > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
private void Jump()
{
if (IsGrounded() == true)
{
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Ladder"))
{
moveSpeed = 4f;
airDec = 5f;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Ladder"))
{
moveSpeed = 12f;
airDec = 0.5f;
}
}
}
This is my movement code using RigidBody2D. I am trying to use the input manager and assigning “Horizontal 1” to player one, and on an identical piece of code, “Horizontal 2” to another player. This however controls both players at the same time.