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 unityya · Aug 17, 2017 at 12:02 AM · scripting problemmovementplayer movementcharacter controller

Player keeps flying up

Hey there! I'm a complete noob in unity and coding and want to make my first own game. I almost finished my movement script and everything works fine. Now i wanted to add a fall animation when the character is not grounded for more than 2 seconds. However, when the fall animation starts, the player seems to lose its gravity and flies up infinitely and I'm having a hard time figuring out why that is. Anyway, here's my script (fall animation is anim integer "AnimPara" = 5):

 public class PlayerMovement : MonoBehaviour {
 
     public float walkBackSpeed = 2f;
     public float walkSpeed = 4f;
     public float runSpeed = 10f;
     public float turnSpeed = 50f;
     public float jumpSpeed = 8f;
     public float speed;
     public float timeTilFall;
     public Animator anim;
 
     private Vector3 moveJump = Vector3.zero;
     private Vector3 moveDir = Vector3.zero;
     private CharacterController controller;
     
 
     void Start () {
         controller = gameObject.GetComponentInChildren<CharacterController>();
         anim = gameObject.GetComponentInChildren<Animator>();
         
     }
     
     void Update () {
 
 
 
         if (Input.GetKey("w"))
         {
             anim.SetInteger("AnimPara", 2);
             speed = walkSpeed;
             if (Input.GetKeyDown("space"))
             {
                 anim.SetInteger("AnimPara", 3);
             }
             if (Input.GetKey("left shift"))
             {
                 anim.SetInteger("AnimPara", 1);
                 speed = runSpeed;
             }
             if (Input.GetKey("s"))
             {
                 anim.SetInteger("AnimPara", 0);
                 speed = 0;
             }
         }
         else if (Input.GetKeyDown("space") && controller.isGrounded)
         {
             anim.SetInteger("AnimPara", 3);
         }
         else if(Input.GetKey("s"))
         {
             anim.SetInteger("AnimPara", 4);
             speed = walkBackSpeed;
         }
         else if (Input.GetKey("s"))
         {
             if (Input.GetKey("w"))
             {
                 anim.SetInteger("AnimPara", 0);
                 speed = 0;
             }
             
         }
         else
         {
             anim.SetInteger("AnimPara", 0);
         }
 
         if (controller.isGrounded)
         {
             anim.SetBool("IsGrounded", true);
             moveDir = transform.forward * Input.GetAxis("Vertical") * speed;
             moveJump = transform.up * Input.GetAxis("Jump") * jumpSpeed;
             timeTilFall = 0;
         }
         else
         {
             timeTilFall += 1 * Time.deltaTime;
             if (timeTilFall > 2 && anim.GetInteger("AnimPara")!=5)
             {
                 anim.SetInteger("AnimPara",5);
             }
         }
         Debug.Log(timeTilFall);
         float turn = Input.GetAxis("Horizontal");
         float jump = Input.GetAxis("Jump");
         transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
         controller.Move(moveDir * Time.deltaTime);
         controller.Move(moveJump * Time.deltaTime);
     }
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 Destolos · Aug 17, 2017 at 07:41 AM 0
Share

Perhaps you should add moveDir = -transform.forward; or something like that in the else-case in line 76.

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.

Follow this Question

Answers Answers and Comments

135 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

Related Questions

Can't declare a joystick 0 Answers

3D RPG - Player movement - He automatically finds the fastest way and goes around everything. How to limit his movement? 1 Answer

Player acts weirdly when I release movement input 0 Answers

Character triple+ Jumping: Please Help! 0 Answers

Do character controllers work with dynamic gravity? 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