Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DivaliciousGeek · May 19, 2020 at 03:47 PM · animationinputgamepad

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");
         }
     }
 }
 

alt text

alt text

capture.png (14.7 kB)
capture2.png (15.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cristiancasallas998 · May 10, 2021 at 05:35 PM 0
Share

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?)

avatar image PhoenixWorldDev · Sep 27, 2021 at 02:32 PM 0
Share

Cristiancasallas998, That's the name of his Input Action.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I fix unity game not working correctly with a controller if it is plugged in before the game starts? 0 Answers

Creating a menu for gamepad? 1 Answer

Unity Editor swallows gamepad input 0 Answers

Input.GetKey 1 Answer

Character rotates erratically when moving with animations. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges