Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Mar 15, 2015 at 05:19 PM by JusticeAShearing for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by JusticeAShearing · Mar 11, 2015 at 05:33 PM · c#movementvector3character controller

Why Is My Movement Script Only Partially Functional?

In my movement script, coded in C#, the horizontal axis controls are fully recognized and their required actions carried out. However, my vertical axis controls are either ignored or not carried out properly.

Specifically, my forward key (inputManager.isInput[0]) does absolutely nothing, while my backward key (inputManager.isInput[1]) performs the jump function of (inputManager.isInput[4]).

This movement script was created by adapting the movement script on the Unity Scripting Reference, that script was found in the page entitled 'CharacterController.Move'.

My adapted script is as follows:

 using UnityEngine;
 using System.Collections;
 
 public class Movement_V : MonoBehaviour {
 
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity;
 
     private float horizontal;
     private float vertical;
 
     private Vector3 moveDirection = Vector3.zero;
 
     public custom_inputs inputManager;
     public Menu_Control_CS menuControl;
 
     void Update()
     {
         CharacterController controller = GetComponent<CharacterController>();
 
         if(menuControl.mainMenuOn == true)
         {
             gravity = 0;
         }
         else
         {
             gravity = 20;
         }

         if(inputManager.isInput[0])
         {
             vertical = -1;
         }
         else if (vertical < 0)
         {
             vertical += 1;
         }
 
         if(inputManager.isInput[1])
         {
             vertical = 1;
         }
         else if (vertical > 0)
         {
             vertical -= 1;
         }
 
         if(inputManager.isInput[2])
         {
             horizontal = -1;
         }
         else if (horizontal < 0)
         {
             horizontal += 1;
         }
 
         if(inputManager.isInput[3])
         {
             horizontal = 1;
         }
         else if (horizontal > 0)
         {
             horizontal -= 1;
         }
 
         if (controller.isGrounded)
         {
             moveDirection = new Vector3(horizontal, 0, vertical);
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
 
             if (inputManager.isInputUp[4])
             {
                 moveDirection.y = jumpSpeed;
             }
             
         }
 
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
 }

The input manager array:

 inputManager.isInput[4]

is used to find when certain keys being pressed. It is the equivalent to:

 Input.GetKey(KeyCode.Space)

Since the horizontal axis is working but the vertical is not, I do not understand why the problem exists. Although I will continue trying to solve this, I would appreciate any help.

Comment
Add comment · Show 1
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 Owen-Reynolds · Mar 11, 2015 at 11:28 PM 0
Share

Throw in lots of debugs. In each if, print("TWO is down");.

If they are held down for a while, make them public in the input manager, so you can see them. Or if it isn't serialized, just copy them: public bool[] I$$anonymous$$_shower=new bool[4]; and each update loop: I$$anonymous$$_shower[i]=input$$anonymous$$anager[i];.

1 Reply

  • Sort: 
avatar image
0

Answer by JusticeAShearing · Mar 15, 2015 at 05:18 PM

It turned out to be a problem with the conversion of 'moveDirection' from moving the player based on the universe's XYZ axes to the player's own XYZ axes.

That conversion took place at line 70, and led to the Z axis having to be represented as the Y axis in the new Vector3 creation at line 69.

 moveDirection = new Vector3(horizontal, vertical, 0);

The coding confusion between the Y and Z axes caused them to swap themselves. Consequently, going forward caused the character to be pushed towards the ground, while the collider wouldn't allow it to fall further. It thus looked like the forward key did nothing. The backward key caused the character to be pushed upwards, thus it performed a jump.

@Owen Reynolds You were right. I found this by using many notations and finding out that the Y axis was being affected by what should have been the Z axis. Consequently, swapping them worked perfectly. Jumping still worked since it wasn't included in the Vector3 creation at line 69.

Comment
Add comment · Show 5 · 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 Owen-Reynolds · Mar 15, 2015 at 05:51 PM 0
Share

Wait -- so your character didn't have Y facing up? That's the only way I can think TransformDirection would cause a problem. It pretty much just says "respect the blue arrow," which is usually what you want.

avatar image JusticeAShearing · Mar 15, 2015 at 06:19 PM 0
Share

I still am unsure about why the problem occurred, but it has been solved, thus I have moved on. Perhaps I will further my efforts to discover the cause of the problem once I have released my game.

The blue arrow faces upwards and affects Y, the green arrow Z and the red arrow X.

avatar image Owen-Reynolds · Mar 16, 2015 at 01:17 AM 0
Share

"The blue arrow faces upwards". There's the problem. A lot of things in Unity think blue=Z=forwards. Your movement script thinks the player is lieing down.

avatar image meat5000 ♦ · Mar 16, 2015 at 01:24 AM 0
Share

Import from Blender? In Blender, Apply the rotation fix or try the new experimental fix button in FBX export.

avatar image JusticeAShearing · Mar 16, 2015 at 05:07 PM 0
Share

@Owen Reynolds Yes, I see that you're correct after looking at several different objects from that same scene. Their blue arrows point forwards, not upwards. While creating my player, the mesh had a different rotation to the Character object (a parent of it) which the script was applied to, while I assumed that they both had the same rotation.

@meat5000 Thanks for the advice, because although this model wasn't a blender import, I will be importing my own blender model soon for purposes of character customization. Thus the advice may be useful in the near future.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Move left etc. according to where player faces 1 Answer

Player isn't affected by AddForce(), Lerp() etc. 1 Answer

Greater than or equal to Vector3 1 Answer

Rotating game object to movement direction 1 Answer


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