- Home /
Input System : Character movemevnt and Jump
Hello, I'm having a problem with the new input system with Unity. The Keyboard works just fine (animation and all) but when I try to use the Gamepad (Xbox 360 controller), the character moves, more like glides while in her idle animation but won't play her walk animation. And when I press the jump button she falls through the floor and just disappears from existence. I'll provide my code and some screenshots, please I seriously need help.
Gamepad Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Gamepad : MonoBehaviour
{
PlayerControls controls;
Vector2 move;
void Awake()
{
controls = new PlayerControls();
controls.Gameplay.Jump.performed += ctx => Jump();
controls.Gameplay.Move.performed += ctx => move = ctx.ReadValue<Vector2>();
controls.Gameplay.Move.canceled += ctx => move = Vector2.zero;
}
void Jump()
{
transform.localPosition *= 5f;
}
void Update()
{
Vector2 m = new Vector2(move.x, move.y) * Time.deltaTime;
transform.Translate(m, Space.World);
}
void OnEnable()
{
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
}
Player Movement Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController2D : MonoBehaviour
{
private bool isGrounded;
[SerializeField]
Transform groundCheck;
[SerializeField]
Transform groundCheckL;
[SerializeField]
Transform groundCheckR;
Animator animator;
Rigidbody2D r2D;
SpriteRenderer spriteR;
[SerializeField]
public float runSpeed = 1.5f;
[SerializeField]
public float jumpSpeed =5;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
r2D = GetComponent<Rigidbody2D>();
spriteR = GetComponent<SpriteRenderer>();
}
void FixedUpdate()
{
if ((Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Ground")))||
(Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Ground"))))
{
isGrounded = true;
}
else
{
isGrounded = false;
animator.Play("Jump");
}
if(Input.GetKey("d")|| Input.GetKey("right"))
{
r2D.velocity = new Vector2(runSpeed, r2D.velocity.y);
if(isGrounded)
animator.Play("Walk");
spriteR.flipX = true;
}
else if (Input.GetKey("a") || Input.GetKey("left"))
{
r2D.velocity = new Vector2(-runSpeed, r2D.velocity.y);
if (isGrounded)
animator.Play("Walk");
spriteR.flipX = false;
}
else
{
if (isGrounded)
animator.Play("Idle");
r2D.velocity = new Vector2(0, r2D.velocity.y);
}
if (Input.GetKey("space")&& isGrounded)
{
r2D.velocity = new Vector2(r2D.velocity.x, jumpSpeed);
animator.Play("Jump");
}
}
}
Answer by jhubbard0221 · Jun 08, 2020 at 07:32 PM
This script should be able to handle movement and jumping with a gamepad; it's what I use for my 3D RPG. Keep in mind, I use Cinemachine's Freelook camera. If you don't use it it could mess up the way the movement script works.
If what I have here isn't enough information ,I suggest you check out this video: https://www.youtube.com/watch?v=4HpC--2iowE
Ask me more questions if you need more specifics, and I might be able to help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class ThirdPersonMovement : MonoBehaviour
{
PlayerControls controls;
Vector3 movement;
public float movementSpeed = 7f;
Vector3 velocity;
public float gravity = -25f;
public float jumpHeight = 1.5f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool isGrounded;
public CharacterController controller;
public Transform cam;
public float turnSmoothTime = 0.07f;
float turnSmoothVelocity;
void Awake()
//Controller input roles
{
controls = new PlayerControls();
controls.Gameplay.Movement.performed += ctx => movement = ctx.ReadValue<Vector2>();
controls.Gameplay.Movement.canceled += ctx => movement = Vector3.zero;
controls.Gameplay.Jump.performed += ctx => Jump();
}
// Update is called once per frame
void Update()
{
//Ground Check
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
//Character Movement
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
//Character Rotation
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * movementSpeed * Time.deltaTime);
}
//Gravity
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
void Jump()
{
if (isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
void OnEnable()
{
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
}
Hey I got this error:( error CS0246: The type or namespace name 'PlayerControls' could not be found (are you missing a using directive or an assembly reference?)
Cristiancasallas998, That's the name of his Input Action.
Your answer
![](https://koobas.hobune.stream/wayback/20220612234732im_/https://answers.unity.com/themes/thub/images/avi.jpg)