Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by LLePrevost · Jun 01, 2018 at 10:58 AM · 3dmovement scriptdirectionfirst-person-controllerjoysticks

How to move in a direction rather than in axis using joysticks?

Hi there, I am very new to Unity and I'm struggling on my current project. I'm basically creating a first person exploration game in which I want to use my controller (gamepad as I've named it) to control my player. I have looked at a variety of resources and created my script which is almost working. My left joystick does make the player walk, A does jump and the right joystick does make him look around.

However an issue I'm having is that my player doesn't walk in the direction my camera is facing. Instead he walks along the x and z axis.

This is the script I've created:

 using UnityEngine;
 
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour
 
 {
     private Vector3 movementVector;
     private float moveHorizontal;
     private float moveVertical;
     private CharacterController characterController;
     private float movementSpeed = 8;
     private float jumpPower = 15;
     private float gravity = 40;
     public float turnSpeed = 50f;
     private Vector3 moveDirection;
 
     void Start()
     {
         characterController = GetComponent<CharacterController>();
     }
 
     void Update()
     {
         Vector3 dir = Vector3.zero;
         Vector3 movement = new Vector3(dir.x, 0, dir.y);
 
         movementVector.z = Input.GetAxis("Gamepad_LeftJoystickX") * movementSpeed;
         movementVector.x = Input.GetAxis("Gamepad_LeftJoystickY") * movementSpeed;
         
         if (characterController.isGrounded)
         {
             movementVector.y = 0;
             if (Input.GetButtonDown("Gamepad_A"))
             {
                 movementVector.y = jumpPower;
             }
         }
         var y = Input.GetAxis("Gamepad_RightJoystickX");
 
         transform.Translate(dir * movementSpeed * Time.deltaTime, Space.World);
 
         transform.Rotate(0, y, 0);
         // Right Stick is mapped to HorizontalR and VerticalR
 
 
         movementVector.y -= gravity * Time.deltaTime;
         characterController.Move(movementVector * Time.deltaTime);
     }
 
 }


Sorry if this does look a bit of a mess, I'm new to coding. Please could someone help me fix this.

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
1

Answer by Harinezumi · Jun 01, 2018 at 11:18 AM

What you are trying to achieve is camera relative movement. Basically you need to get a reference to your viewing camera (can be Camera.main if that is the one), and multiply movementVector.z with that camera's transform.forward and movementVector.xwith transform.right. Check out this forum post for a more details, or this answer (although the formatting is a mess).
Note that with this change transform.Rotate() will not turn your character in the desired direction: you will have to get the angle at which you point the right joystick ( Mathf.Atan2() is very useful for this), and then rotate relative to the camera's transform.forward.

BTW, I noticed that you move both by Transform.Translate() and CharacterController.Move(). Don't do this, you will get conflicting results, because transform.Translate() ignores physics, while CharacterController uses it. This didn't cause you problems, because dir is zero-vector, but it could have. Just remove the variable dir and transform.Translate() call, and directly set movement to Vector3.zero, and you should be fine.

Comment
Add comment · Show 6 · 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 LLePrevost · Jun 01, 2018 at 12:54 PM 0
Share

Ok I'm trying it out, how do I multiply the movementVector with the camera's transform.forward because it seems to come up with an error when I try it like this.

             movementVector.z * transform.forward = Input.GetAxis("Gamepad_LeftJoystickX") * movementSpeed;

Seems that the line comes up as an error

avatar image Harinezumi LLePrevost · Jun 01, 2018 at 01:05 PM 0
Share

You cannot have an instruction on the left side of =. Check out the linked forum post for working code example, but it should be something like this:

 movementVector = Camera.main.transform.forward * Input.GetAxis("Gamepad_LeftJoystickX") * movementSpeed;

Note that you might want to project the camera's forward vector onto the xz plane by doing this:

 Vector3 cameraForward = Camera.main.transform.forward;
 Vector3 projectedForward = new Vecto3(cameraForward.x, 0, cameraForward.z).normalize;

Unfortunately, the above will give you an invalid vector if your camera is looking directly up or down (forward is (0, 1, 0) or (0, -1, 0)). But you should try to avoid that situation anyway.
Forum post:
https://forum.unity.com/threads/moving-character-relative-to-camera.383086/

avatar image LLePrevost Harinezumi · Jun 01, 2018 at 01:26 PM 0
Share
 Camera.main.transform.forward * Input.GetAxis("Gamepad_LeftJoystickX") * movementSpeed

This section has been errored, stating "Cannot implicitly convert type 'UnityEngine.Vector3' to 'float'. I've made the changes and this is the only error that's come up. Could I missing something that should of been stated near the beginning of my script.

Show more comments
Show more comments

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

100 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

Related Questions

Particle emission : Give each particle a specific direction 1 Answer

How can i normalize 2d Vectors? 2 Answers

How to change player direction with one button?,How do I make one button change direction of player? 0 Answers

How would I restrict Movement in Air? 1 Answer

How to make a 3rd person character controller 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