I am trying to make a movement/controller script from scratch. The reason I am doing this is that in my unity course they can have given me every asset including scripts. There is an input manager script and a controller script provided by my instructor. The input manager script is using unity's new input system package. However, I wanted to make my own controller script with new characters. So, I started writing my own movement script but with the given input manager script.
So the problem is I cannot run and jump at the same time. But also what my player does is that it can jump and then run which seems like it is floating and slowly coming down. I have tried to manipulate the linear drag and gravity but eventually do the same thing. I don't know if it's a FixedUpdate() issue or an input issue? or else something is wrong with the physics that I am not understanding. Can anyone try to help me? I am kind of new to unity and C#, that's why I am taking a course.
public class Test_mov : MonoBehaviour
{
private Rigidbody2D rb2d;
public float moveSpeed;
public float jumpForce;
private bool isjumping;
private InputManager inputManager;
private Animator animator;
private bool FacingRight = true;
// Start is called before the first frame update
void Start()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
isjumping = false;
}
// Update is called once per frame
void Update()
{
//moveHorizontal = inputManager.horizontalMoveAxis;
//moveVertical = inputManager.verticalMoveAxis;
SetupInput();
}
private void FixedUpdate()
{
jump();
if (inputManager.horizontalMoveAxis > 0.1f || inputManager.horizontalMoveAxis < -0.1f)
{
//rb2d.AddForce(new Vector2(inputManager.horizontalMoveAxis * moveSpeed, 0f), ForceMode2D.Impulse);
rb2d.velocity = new Vector2(inputManager.horizontalMoveAxis * moveSpeed, 0f);
}
//if (!isjumping && inputManager.verticalMoveAxis > 0.1f)
//{
// rb2d.AddForce(new Vector2(0f, inputManager.verticalMoveAxis * jumpForce), ForceMode2D.Impulse);
// rb2d.velocity = new Vector2(0f, inputManager.verticalMoveAxis * jumpForce);
//}
if (inputManager.horizontalMoveAxis > 0 && !FacingRight)
{
Flip();
}
else if (inputManager.horizontalMoveAxis < 0 && FacingRight)
{
Flip();
}
animator.SetBool("run", inputManager.horizontalMoveAxis != 0);
//if(inputManager.horizontalMoveAxis > 0)
//{
// gameObject.transform.localScale = new Vector3(1, 1, 1);
//}
//else if (inputManager.horizontalMoveAxis < 0)
//{
// gameObject.transform.localScale = new Vector3(-1, 1, 1);
//}
}
private void SetupInput()
{
if (inputManager == null)
{
inputManager = InputManager.instance;
}
if (inputManager == null)
{
Debug.LogWarning("There is no player input manager in the scene, there needs to be one for the Controller to work");
}
}
void jump()
{
if(!isjumping && inputManager.verticalMoveAxis > 0.1f)
{
rb2d.AddForce(new Vector2(0f, inputManager.verticalMoveAxis * jumpForce), ForceMode2D.Impulse);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Platform")
{
isjumping = false;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Platform")
{
isjumping = true;
}
}
void Flip()
{
//Vector3 currentScale = gameObject.transform.localScale;
//currentScale.x *= -1;
//gameObject.transform.localScale = currentScale;
transform.Rotate(0f, 180f, 0f);
FacingRight = !FacingRight;
}
public bool canAttack()
{
return inputManager.horizontalMoveAxis == 0 && isjumping;
}
}
CodePudding user response:
Thats happening because you set y velosity to 0 after jump. Just change only x velocity, when you handling movement
private void FixedUpdate()
{
jump();
if (inputManager.horizontalMoveAxis != 0)
{
rb2d.velocity = new Vector2(inputManager.horizontalMoveAxis * moveSpeed, rb2d.velocity.y);
}
// Other stuff...
}
Actually, if you want use physics-based movement, better you should use
_rb2d.AddForce(direction * moveSpeed * Time.fixedDeltaTime, ForceMode.Force)
And just play with angular drag and speed parametrs.
Or use CharacterController
, if you won't use rigidbody physics
CharacterController
_controller.Move(direction * Time.deltaTime * playerSpeed);
CodePudding user response:
In the FixedUpdate method. you should change the r2bd.velocity line. Currently while running you are assigning to the y-axis 0f and while jumping you are assigning the x-axis 0f. So they are neutralizing each other. You mustn't reset the other axis. If you write gameObject.velocity.y or gameObject.velocity.x to the unused side of the code, your problem will be fixed.
if (inputManager.horizontalMoveAxis > 0.1f || inputManager.horizontalMoveAxis < -0.1f)
{
//rb2d.AddForce(new Vector2(inputManager.horizontalMoveAxis * moveSpeed, 0f), ForceMode2D.Impulse);
rb2d.velocity = new Vector2(inputManager.horizontalMoveAxis * moveSpeed, rb2d.velocity.y);
}
//if (!isjumping && inputManager.verticalMoveAxis > 0.1f)
//{
// rb2d.AddForce(new Vector2(0f, inputManager.verticalMoveAxis * jumpForce), ForceMode2D.Impulse);
// rb2d.velocity = new Vector2(rb2d.velocity.x, inputManager.verticalMoveAxis * jumpForce);
//}
and I have a suggestion for you. You should check the unity input system extension. it will make your job much easier.