• 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 /
  • Help Room /
avatar image
Question by jhubbard0221 · May 24, 2020 at 11:36 PM · movementinput3ddirectiongamepad

Making My Character Face The Direction They Are Moving (3D RPG, Unity Input System)

I have recently cracked being able to get my 3D character to move in a 3D world using a gamepad through Unity's Input System. However, no matter which direction they move, they always face the same direction. I just want them to be able to face the direction they are moving and keep facing the direction they were in when they stop moving. I don't know if the Input System and my current code changes how that is done, so I'm posting the code I have just to be safe. If anyone knows a script that can work for me, please let me know.

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.EventSystems;
  using UnityEngine.InputSystem;
  using UnityEngine.Scripting.APIUpdating;
  
  public class PlayerController : MonoBehaviour
  {
      PlayerControls controls;
  
      Vector3 Movement;
  
      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;
      }
  
      //Joystick controls
      void Update()
      {
          //Character Movement
          Vector3 m = new Vector3(Movement.x, 0, Movement.y) * Time.deltaTime;
          transform.Translate(m, Space.World);
      }
  void OnEnable()
      {
          controls.Gameplay.Enable();
      }
  void OnDisable()
      {
          controls.Gameplay.Disable();
      }
  }
Comment

People who like this

0 Show 0
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

Answer by jhubbard0221 · Jun 05, 2020 at 06:06 PM

For those following the question, I figured it out. For anyone wanting good third person movement with a gamepad, the YouTube channel Brackeys has two two videos out that you can use to get the results you want.

Gamepad Input Tutorial (Need Input System): https://www.youtube.com/watch?v=p-3S73MaDP8

Third Person Movement (Need Cinemachine): https://www.youtube.com/watch?v=4HpC--2iowE

Following this will allow you to make a character that moves around relative to the camera. They will face the direction they are moving and smoothly transition between directions they are facing. The only issue I have left is I cannot control the camera movement with the right joystick yet, but I have already posted a question about that.

By the end of it I had a movement script that looked like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class ThirdPersonMovement : MonoBehaviour
 {
     PlayerControls controls;
 
     Vector3 Movement;
 
     public CharacterController controller;
     public Transform cam;
 
     public float movementSpeed = 6f;
 
     public float turnSmoothTime = 0.1f;
     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;
     }
 
     // Update is called once per frame
     void Update()
     {
         //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);
         }
     }
 
     void OnEnable()
     {
         controls.Gameplay.Enable();
     }
 
     void OnDisable()
     {
         controls.Gameplay.Disable();
     }
 }
 

Feel free to copy and paste this, just know that you will need the Input System and the Cinemachine Freelook camera. If anyone has any questions about this, let me know, I might be able to help.

Comment

People who like this

0 Show 1 · 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 Epicgamer6999 · Oct 29, 2020 at 03:11 PM 0
Share

why cant i use PlayerControl?? and also the Unityengine input system

thx

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

311 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 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 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

Cinemachine Freelook Camera Movement with Joystick 3 Answers

Character Jump only works randomly and only with the "Y" Button 0 Answers

What is the key mapping for a Logitech Extreme 3D Pro joystick in Unity? 0 Answers

Gamepad input in UI stop working after a new panel cover the first one 0 Answers

How to Rotate player on Y Axis using Mouse position, but also Clamp the rotation distance? 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