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 /
avatar image
0
Question by Payaso_Prince · Jul 15, 2020 at 04:00 PM · charactercontrollercharacter controllercharacter.movecharacter control

Character Controller Script Increasing characters Y position

Hello all!

I am having an issue with my character controller script. It is working perfectly except for one issue. Whatever object I attach it to has its Y position increased by 8 at runtime. Essentially, my Idlestate script updates myVector and then my player controller script runs myController.Move(myVector).

I have a variable for velocity which I set myVector.y = to. This is my first time posting a question and I apologize if the formatting is off. Any help would be much appreciated!

 public class PlayerController : MonoBehaviour
 {
     
 
     // Create an instance of the statemachine
     public HStateMachine stateMachine = new HStateMachine();
 
      // Start is called before the first frame update
     void Start()
     {
         // Start the state off in the Idle State
         stateMachine.ChangeState(new HIdleState(this));
         
         // Reference to the animator
         anim = GetComponent < Animator>();
     }
 
 
     // Character Controller reference
     public CharacterController myController;
 
     // Camera reference
     // A transform is a reference to somethings position, rotation, and scale.
     public Transform cameraTransform;
 
     // Animator reference
     public Animator anim;
 
     // Bool used to tell which direction the player is facing
     public bool facingRight = false;
 
     // Player movement Speed
     public float speed = 3f;
 
     // Player Jump speed
     public float jumpSpeed = 10f;
 
     // Gravity affecting Player
     public float gravityStrength = 5f;
 
     // If player is on the ground they are able to jump
     public bool canJump = false;
 
     // Create a vertical velocity to use for implementing gravity and jumping.
     public float verticalVelocity;
 
     // Player's Vector
     public Vector3 myVector;
 
     // Collision Flags to detect if the player is grounded
     public CollisionFlags flags;
 
     // Update is called once per frame
     void Update()
     {
        
         // Run the State Machine and handle the current state.
         stateMachine.Update();
 
         
         // Flip the player's sprite by rotating the x value 180 degrees
         if ( (myVector.x < 0 && facingRight) || (myVector.x > 0 && !facingRight) )
         {
             facingRight = !facingRight;
             transform.Rotate(new Vector3(0, 180, 0));
         }
 
       
 
 
         //myVector.y = verticalVelocity * Time.deltaTime; // add the new vertical velocity to player to simulate gravity.
 
         // Use Input to move the character
         // Move the character Controller and store the reference in a collision flag. Velocity along the y-axis is ignored. Gravity is automatically applied.
         Debug.Log(myVector.y);
         flags = myController.Move(myVector);
 
         
         
         }
 
     
 
 }


Idle state script:

 public class HIdleState : HPlayerState
 {
     
     // Reference Player Controller
     PlayerController playerController;
     
 
     public HIdleState(PlayerController playerController)
     {
         // Point to the PlayerController passed in.
         this.playerController = playerController;
     }
     public override void Enter()
     {
         Debug.Log("entering Idle state");
     }
     
     public override void Execute()
     {
         // Debug.Log("updating Idle state");
 
         // Vector3 for the Player.
         playerController.myVector = new Vector3(0, 0, 0);
 
 
         // Get Input from the player
         playerController.myVector.x = Input.GetAxisRaw("Horizontal");
         playerController.myVector.z = Input.GetAxisRaw("Vertical");
         playerController.myVector = Vector3.ClampMagnitude(playerController.myVector, 1f);
 
         
         // Add speed to the player.
         // Multipling something by time.deltatime converts it from moving an amount per frame to an amount per second.
         playerController.myVector = playerController.myVector * playerController.speed * Time.deltaTime;
 
         
         // Animate the player if they are walking and grounded.
         if ((Math.Abs(playerController.myVector.x) > 0 && playerController.canJump) || (Math.Abs(playerController.myVector.z) > 0 && playerController.canJump))
         {
             playerController.anim.SetBool("Walking", true);
         }
         else
         {
             playerController.anim.SetBool("Walking", false);
         }
 
         // Check the collision on CharacterController.Move to know if the player is grounded.
         if ((playerController.flags & CollisionFlags.Below) != 0)
         {
             // The player is grounded so set jumping bool in the animator to false.
             playerController.anim.SetBool("Jumping", false); 
             
             playerController.canJump = true;
             playerController.verticalVelocity = -3f;
         }
         else
         {
              playerController.canJump = false;
         }
 
         // Add new speed to old speed for acceleration on the vertical velocity.
         playerController.verticalVelocity = playerController.verticalVelocity - (playerController.gravityStrength * Time.deltaTime);
 
         // If the Jump button is pressed change to the Airborne State.
         if (Input.GetButtonDown("Jump"))
         {
             if (playerController.canJump == true)
             {
                 playerController.stateMachine.ChangeState(new HAirborneState(playerController));
                 
             }
         }
         
         // Set the player's y vector to verticalVelocity to know how much to move.
         playerController.myVector.y = playerController.verticalVelocity * Time.deltaTime;
 
     }
 
     public override void Exit()
     {
         Debug.Log("exiting Idle state");
     }
     
 
 }


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 TheShrikeFromCodeStudio · Jan 23 at 07:21 PM

I had the same problem and found your unanswered post. After a lot of research I came across this video which was very helpful.

https://www.youtube.com/watch?v=UUJMGQTT5ts

Basically my problem was the setting in the character controller for "skin width". This is a buffer area around the the collision capsule that will trigger a collision with things such as the ground, Mine was set too high for the scale of my world and thus noticeable raised my character off the ground. It should be set to some ratio of the controller capsule "radius" setting. The recommendation was 10% but had a radius of 0.05 and set siskin width to .001 which seemed to fix my issue.

Hope this helps some one in the future.

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

149 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

Related Questions

why does character controller accelerate off ledges? 1 Answer

Character Controller Slope Limit on Terrain 0 Answers

ControllerColliderHit: best way to check if is ground 0 Answers

Why do CharacterController objects not come in different collision shapes? 0 Answers

CharacterController isGrounded confusion 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