• 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 MachCUBED · Oct 12, 2012 at 02:30 AM · javascriptmovementcharactercontrollercontroller

CharacterController unexpectedly moving up

CharacterController unexpectedly moving up when Move() is called, stays at same elevated y position except when jumping, returns to elevated y position when landing

Hi guys,

I have a CharacterController in a gameobject that's based on the Camera Relative Controls prefab from Standard Assets (Mobile). The Update() function has been modified to look like t$$anonymous$$s, with the included helper functions (I wanted to keep my Update() function as clean as possible):

 function Jump()
 {
     // Apply the current movement to launch velocity
     velocity = character.velocity;
     velocity.y = jumpSpeed;
     canJump = false;
 }
 
 function GetButtonInputs()
 {
     // Button inputs
 
     var tapCount = Input.touchCount;
 
     for ( var i = 0 ; i < tapCount ; i++ ) 
     {
 
         var touch = Input.GetTouch(i);
 
         // Now handle button touches
 
            if(touch.phase == TouchPhase.Began && jumpButton.HitTest(touch.position))
            {
              if ( character.isGrounded )
                  Jump();
             }
         //TODO: Add use buttons
     }
 }
 
 function Update()
 {
     var movement = cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );
     // We only want the camera-space horizontal direction
     movement.y = 0;
     movement.Normalize(); // Adjust magnitude after ignoring vertical movement
     
     // Let's use the largest component of the joystick position for the speed.
     var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
     movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
     
     // Check for jump using right stick
     if ( character.isGrounded )
     {
         Debug.Log("Player grounded");
         if ( !rotateJoystick.IsFingerDown() )
             canJump = true;
     }
     else
     {            
         // Apply gravity to our velocity to diminish it over time, only do so when not in water.
         velocity.y += Physics.gravity.y * Time.deltaTime;
         
         // Adjust additional movement w$$anonymous$$le in-air
         movement.x *= inAirMultiplier;
         movement.z *= inAirMultiplier;
     }
     
     GetButtonInputs();
     
     // Move player
     
     movement += velocity;
     
     movement += Physics.gravity;
         
     movement *= Time.deltaTime;
     
     Debug.Log("Movement x" + movement.x + "Movement y" + movement.y + "Movement z" + movement.z);
     
     // Actually move the character
     character.Move( movement );
     
     if ( character.isGrounded )
         // Remove any persistent velocity after landing
         velocity = Vector3.zero;
     
     // Face the character to match with where she is moving
     FaceMovementDirection();    
     
     // Handle camera rotation
     
     // Handle camera movement swipes
     
     for (var touch : iPhoneTouch in iPhoneInput.touches) 
     { 
         if (touch.phase == iPhoneTouchPhase.Moved)
         {
                var FingerDidMove=true;
         }    
     }
     
     // Scale joystick input with rotation speed
     
     var camRotation = rotateJoystick.position;
     camRotation.x *= rotationSpeed.x;
     camRotation.y *= rotationSpeed.y;
     camRotation *= Time.deltaTime;
     
     // Rotate around the character horizontally in world, but use local space
     // for vertical rotation
     cameraPivot.Rotate( 0, camRotation.x, 0, Space.World );
     cameraPivot.Rotate( camRotation.y, 0, 0 );
 }

When I run my game, the player GameObject (w$$anonymous$$ch has a shadow blob projector and my mesh as its c$$anonymous$$ldren) starts off at the position set in the editor, including the y position. However, when I move, the player GameObject (w$$anonymous$$ch has a CharacterController component) suddenly changes its y position to a level that makes my player character appear to be floating $$anonymous$$s own height above the ground. The player GameObject that has both the blob projector and the mesh as its c$$anonymous$$ldren (the one with the Character Controller) attached is the one that moves up, not the mesh. Moreover, there are no other colliders on the Player mesh or any of its c$$anonymous$$ldren, so consequently, changing my collision matrix to disallow Player/Player collisions (The player character is on its own layer, namely Player) didn't fix anyt$$anonymous$$ng. T$$anonymous$$s problem appears both in the editor and when I deploy the game to my iPad. The player can still jump, but the player lands at the same elevated height that I'm having problems with. What seems to be causing it to get elevated?

MachCUBED

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 MachCUBED · Oct 24, 2012 at 05:05 AM 0
Share

0 Replies

· Add your reply
  • Sort: 

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Character Controller moves forever after colliding with object 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Character rolls consistently in all directions, but right. 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