So I chose to learn unity and C# as a project for my individual study class and the due date is Monday next week and I been having problems getting my player character to stop. So far, each solution I have tried resulted in no change or a error.
using UnityEngine;
using UnityEngine.InputSystem;
public class Player_Movement : MonoBehaviour
{
// Using 2d as game is controled on X,Y
//Public eaisy to edit
public Rigidbody2D rb2d;
public float yForce = 100f;
public float xForce = 100f;
public Transform groundCheck;
public LayerMask groundLayer;
float direction = 0;
//private to avoid changing
private bool isGrounded;
//Cheacks for controls and enables controler support all new controls aspects need to be added below here but above the Void Awake function.
InputAction jumpAction;
PlayerControls controls;
private void Awake()
{
controls = new PlayerControls();
controls.Enable();
//This was only needed for -/+ axis thank god
controls.Land.Movement.performed += ctx =>
{
direction = ctx.ReadValue<float>();
};
//Formating for all future Imput actions below "_Action = InputSystem.actions.FindAction("_");
jumpAction = InputSystem.actions.FindAction("JumpingKeys");
// Jumping Input action
}
private void OnCollisionEnter2D(Collision2D collision)
{
//Ground collision to check if jumping is possible, make sure to apply Tag=Ground to anything I want the player to jump off of.
if (collision.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void FixedUpdate ()
{
//Basic movement
//Need to figure out a way to make this reset Velocity to zero when nuter at a contolled pase or
// switch to AddForce2D which can lead to hole host of problums later. (problum below)
rb2d.linearVelocity = new Vector2(direction * xForce, rb2d.linearVelocity.y);
if (jumpAction.IsPressed())
{
if(isGrounded)
{
rb2d.AddForce(Vector2.up * yForce);
}
}
//Jumping
}
}
Any help will be appreciated. I was unable to get help earlier because posting on forums is banned on school WIFI.