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
0
Question by GNetto · Apr 03, 2018 at 07:07 PM · gameobjecttransformvector3controller

Unwanted gameObject movement after jumping.

Pretense:

~~I want to stay away from Input.GetAxis as it only caused me issues when developing the movement I wanted.

~~Ballistic jumping is intentional.

I'm trying to imitate the character movement from WoW. I've got the feel of the movement and jumping down, but my gameObject was only moving along the world axis. So I've tried to localize the character movement and it works well... while I'm on the ground.

If I jump while my gameObject's rotation is 0, the gameObject will move and jump just fine. However, if there is ANY rotation on the gameObject, my character will jump but will arc away from the axis i was jumping on. I know which piece of code is causing the unwanted movement, but that piece of code is also what allows the gameObject to move freely off of the world axis.

 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     #region Variables
     // Allows access to CharacterController component //
     private CharacterController controller;
 
     public float turnSpeed;
     public float moveSpeed;
     public float jumpForce;
     public float gravity;
 
     private Vector3 moveDir = Vector3.zero;
     #endregion
 
     private void Start()
     {
         // Sets controller for use of CharacterController component //
         controller = gameObject.GetComponent<CharacterController>();
     }
 
     void Update()
     {
         // Requires player to be touching the ground //
         if (controller.isGrounded)
         {
             moveDir = new Vector3(0, 0, 0); // Stops movement if no input //
 
             PlayerMovement(KeyCode.W, Vector3.forward, moveSpeed);
             PlayerMovement(KeyCode.Q, Vector3.left, moveSpeed);
             PlayerMovement(KeyCode.S, Vector3.back, moveSpeed);
             PlayerMovement(KeyCode.E, Vector3.right, moveSpeed);
 
             PlayerMovement(KeyCode.A, Vector3.down); // Basic turning movement //
             PlayerMovement(KeyCode.D, Vector3.up);   //                        //
 
             JumpMovement();
         }
 
         // Applies gravity to the controller //
         moveDir.y -= gravity * Time.deltaTime;
 
         // Sets movement local to player //
         //                                          //
         //                                          //
         //                                          //
         // This is causing the air movement problem //
         moveDir = transform.TransformDirection(moveDir);
         // Moves player //
         controller.Move(moveDir * Time.deltaTime);
     }
 
     // Controls player movement (QWSE) //
     void PlayerMovement(KeyCode key, Vector3 vector, float moveSpeed)
     {
         if (Input.GetKey(key))
         {
             moveDir += vector * moveSpeed;
         }
     }
 
     // Controls player turning (AD) //
     void PlayerMovement(KeyCode key, Vector3 vector)
     {
         if (Input.GetKey(key))
         {
             controller.transform.Rotate(vector * turnSpeed);
         }
     }
 
     // Controls player jumping mechanic //
     void JumpMovement()
     {
         if (Input.GetKey(KeyCode.Space))
         {
             moveDir.y = jumpForce;
         }
     }
 
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 GNetto · Apr 03, 2018 at 07:26 PM

I got it fixed before anyone got to comment, lol.

For anyone caring to know what the fix was, all I did was move moveDir = transform.TransformDirection(moveDir); right after when JumpingMovement(); is called in the update.

I assume what this does is makes the localization only happen while the gameObject is grounded, but after any input has been delivered to change the direction.

Comment
Add comment · 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

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

137 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

Related Questions

Rotating a Vector3 in Instantiation 1 Answer

XBox Controller Angle of Fire Incorrect 0 Answers

Finding the Current Vector of Travel 1 Answer

Instantiate GameObject towards player 0 Answers

Cannot precisly get the Transform(position and rotation) and velocity of the HTC vive controller in Unity 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