• 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 /
  • Help Room /
avatar image
Question by sporkflips · Mar 06, 2016 at 06:25 AM · c#gravityjumpingfloats

Floats are acting really weird

I've been having trouble getting the floats that I assign to my movement speed variables to result in any consistent impact on my character...

I'm not sure what's going on, the gravity that I implemented works but it falls REALLY slowly unless I multiple its speed by about 100. It goes between
falling really slowly at moveDirection.y -= gravity *60
to clipping down in chunks before falling the rest of the way slowly at gravity *70 or 80
and then by the time it's at gravity *100 it just clips straight down to the ground...

I had previously just allowed it to be at that speed where it clips so that the character would at least remain grounded (since there aren't really any drops in the scene I'm working on, I just was having trouble with .isGrounded before so I had the character raised for testing purposes) but it's making it basically impossible to implement a working jump.

I'll include the code I'm working on; there are some things in there that I haven't finished yet that are commented out just because I'm eventually going to reimport the character model once I fix its animations (add falling animation and update run cycle) and then I also left in an alternate jump I was messing with which is also commented out.

I'd appreciate any suggestions about any part of it that anyone might have

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour
 {
     private GameObject thePlayer;
     private Animator anim;
     private CharacterController controller;
     Transform myTransform;
     Vector3 moveDirection = Vector3.zero;
     PlayerEnergy playerEnergy;
 
     public CollisionFlags _collisionFlags;
 
     public float gravity = 60.0f;
     public float speed = 3.0f;
     public float turnSpeed = 80.0f;
     public float jumpHeight = 60;
     public float jumpLength = 1.5f;
     public float fallAfter = 0.5f;
 
     private float _timeToGo1;
     private float _timeToGo2;
 
     public bool playerStop;
     public bool playerStill;
     public bool jumping;
     public bool lyingDown;
     private bool _hasBeenLaying;
     public bool playerResting;
     public float airTime = 0;
 
 
 
     void Start()
     {
         Debug.Log("Initialized");
 
         //search for player clones and remove them
         GameObject[] remaining = GameObject.FindGameObjectsWithTag("GoblinPlayer");
         foreach (GameObject clone in remaining)
         {
             if (clone.name == "GoblinCharacter(Clone)")
             {
                 GameObject.Destroy(clone);
             }
         }
 
 
         thePlayer = GameObject.FindGameObjectWithTag("GoblinPlayer");
         anim = thePlayer.GetComponent<Animator>();
         controller = thePlayer.GetComponent<CharacterController>();
         myTransform = thePlayer.transform;
         playerEnergy = thePlayer.GetComponent<PlayerEnergy>();
 
         _timeToGo1 = Time.fixedTime + 5.0f;
         _timeToGo2 = Time.fixedTime + 30.0f;
 
         playerStop = true;
         playerStill = true;
         lyingDown = false;
         _hasBeenLaying = false;
         playerResting = false;
         jumping = false;
     }
 
 
     IEnumerator WaitOneSecond()
     {
         yield return new WaitForSeconds(1);
     }
 
 
     //restore 3% of energy up to 100
     void RestingEnergy()
     {
         playerEnergy.curEnergy = Mathf.Clamp(playerEnergy.curEnergy + 3.0f, 0.0f, (playerEnergy.maxEnergy));
     }
 
 
     void FixedUpdate()
     {
         //update once every 5 seconds
         if (Time.fixedTime >= _timeToGo1)
         {
             Debug.Log("W is pressed: " + Input.GetKey(KeyCode.W));
             Debug.Log("moveDirection is " + moveDirection);
             Debug.Log("playerEnergy is " + playerEnergy.curEnergy);
             Debug.Log("playerStill is " + playerStill);
             Debug.Log("lyingDown is " + lyingDown);
             Debug.Log("playerResting is " + playerResting);
             Debug.Log("jumping is " + jumping);
             
             //restore energy if the player is resting
             if (playerResting == true)
             {
                 RestingEnergy();
             }          
             _timeToGo1 = Time.fixedTime + 5.0f;
         }
     }
 
     void Update()
     {
         moveDirection = myTransform.forward * Input.GetAxis("Vertical") * speed;
         float turn = Input.GetAxis("Horizontal");
 
 
         if (controller.isGrounded)
         {
             //Debug.Log("we're on the ground");
             airTime = 0;
             
             anim.SetBool("DoesJump", false);
             if (Input.GetKeyDown(KeyCode.Space) && lyingDown == false)
             {
                 if (airTime < jumpLength)
                 {
                     moveDirection.y = (moveDirection.y - gravity) + jumpHeight;
                     anim.SetBool("DoesJump", true);
                 }
             }
             /*
             if (jumping == false && lyingDown == false && Input.GetKeyDown(KeyCode.Space))
             {
                 anim.SetBool("DoesJump", true);
                 moveDirection.y += jumpHeight;
                 jumping = true;
                 WaitOneSecond();
                 anim.SetBool("DoesJump", false);
                 moveDirection.y -= jumpHeight;
                 jumping = false;
             }
             */
 
             //anim.SetBool("IsFalling", false);
 
             if (lyingDown == false && jumping == false)
             {
                 if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift))
                 {
                     speed = 20.0f;
                     anim.SetInteger("MotionTrigger", 2);
                     playerEnergy.energyDrainSpeed = 40;
                     playerStop = false;
                     playerStill = false;
                     playerResting = false;
                 }
                 else if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.S))
                 {
                     speed = 7.0f;
                     anim.SetInteger("MotionTrigger", 1);
                     playerEnergy.energyDrainSpeed = 8;
                     playerStop = false;
                     playerStill = false;
                     playerResting = false;
                 }
                 else if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W))
                 {
                     speed = 5.0f;
                     anim.SetInteger("MotionTrigger", 4);
                     playerEnergy.energyDrainSpeed = 7;
                     playerStop = false;
                     playerStill = false;
                     playerResting = false;
                 }
                 else
                 {
                     anim.SetInteger("MotionTrigger", 0);
                     playerEnergy.energyDrainSpeed = 0;
                     playerStop = true;
                 }
             }
 
             //lay down when the player presses the R key and is not moving
             if (playerStop == true)
             {
                 if (lyingDown == false && Input.GetKeyDown(KeyCode.R))
                 {
                     anim.SetBool("LayingDown", true);
                     lyingDown = true;
                 }
                 else if (lyingDown == true && Input.GetKeyDown(KeyCode.R))
                 {
                     anim.SetBool("LayingDown", false);
                     lyingDown = false;
                 }
             }
 
 
             //check for lyingDown once every 30 seconds
             if (Time.fixedTime >= _timeToGo2)
             {
                 _timeToGo2 = Time.fixedTime + 30.0f;
                 if (lyingDown == true)
                     playerStill = true;
             }
             //has the player been lying down for 30 seconds?
             if (playerStill == true && lyingDown == true)
                 playerResting = true;
 
         } else
         {
             airTime += Time.deltaTime;
             /*
             if (airTime >= fallAfter)
             {
                 anim.SetBool("IsFalling", true);
             }*/
         }
 
         moveDirection.y -= gravity * Time.deltaTime;
         if (lyingDown == false)
         {
             _collisionFlags = controller.Move(moveDirection * Time.deltaTime);
             transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
         }
     }
 }



Comment

People who like this

0 Show 0
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

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

125 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

Related Questions

Uneven Jump Heights 1 Answer

Cube Flying Upwards when it should fall down!,Cube flying upwards when gravity should be happening! Help! 1 Answer

why is my jump script not working? 0 Answers

Followed Brackey's 1st person movement tutorial but the jump mechanic won't work and sometimes neither will the gravity 0 Answers

player not able to double jump 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