• 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
Question by rebbrun · Jun 18, 2016 at 05:36 AM · collidercontrollerjumpingplatformer

Character Instantly hits Ground after Falling off Object

Hello, so close to getting a decent character controller. For testing I set up some default cubes up and have been jumping up them. The problem is when I walk off the cube (not jump), my character instantly hits the ground plane. I've tried adjusting the scale of my character model as well as change the gravity setting in Unity. Neither has impacted the speed of which my character falls to the floor. Help is greatly appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class playerScript : MonoBehaviour
 {
     //Public Settings
     public float speed = 20.0f;
     public float jumpForce = 60.0f;
     public float gravity = 60.0f;
     public float dist = 0.1f;
 
     public LayerMask ground;
 
     //Private Settings
     private Vector3 moveDirection = Vector3.zero;
     private CharacterController controller;
     private Animator anim;
 
     void Start()
     {
         //Setting up animation/character controls
         controller = GetComponent<CharacterController>();
         anim = GetComponentInChildren<Animator>();
     }
 
     void FixedUpdate()
     {
         //Input
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         bool jump = Input.GetButton("Jump");
 
         Movement(h, v, jump);
     }
 
     void Movement(float h, float v, bool jump)
     {
         //Calls rotation function in order to determine which direction is forward.
         //Moves character forward.  Not limited to isGrounded so that character can manipulate falling direction.
         if (h != 0 || v != 0)
         {
             Rotation(h, v);
             anim.SetInteger("animPar", 1);
             moveDirection.z = v * speed;
             moveDirection.x = h * speed;
         }
         //If character is not moving at all, zeros forward/side motion (potential problem area perhaps?)
         else if (h == 0 && v == 0 && controller.isGrounded)
         {
             anim.SetInteger("animPar", 0);
             moveDirection.z = v * 0;
             moveDirection.x = h * 0;
         }
         //Initializes the jump function.  Only can be done as long as the character isGrounded.
         if (controller.isGrounded && jump == true)
         {
             Jump(jump);
         }
         //Ran into an issue where I couldn't get the jump animation to play consistently if moving and jumping.  So I set this value to always
         //play the jump animation whenever character is not grounded.
         if (!controller.isGrounded)
         {
             anim.SetInteger("animPar", 2);
         }
         //Moves character.
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(Camera.main.transform.TransformDirection(moveDirection) * Time.deltaTime);
     }
 
     void Rotation(float h, float v)
     {
         //Not only makes character always face direction of travel, but also re-calculates the forward transform directon based on the rotation of
         //my camera.
         Vector3 direction = new Vector3(h, 0f, v);
         Quaternion rotation = Quaternion.LookRotation(direction, Vector3.up);
         transform.rotation = rotation * Camera.main.transform.rotation;
     }
 
     void Jump(bool jump)
     {
             //Super basic jump function.  Nothing fancy.  Probably will try to add double jump eventually, but that's a different story...
             anim.SetInteger("animPar", 2);
             moveDirection.y = jumpForce;
     }
 }
     


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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by tanoshimi · Jun 20, 2016 at 04:41 PM

Your time-compensation looks very odd....

Line 66 you're adjusting (the y component only?) of moveDirection with respect to Time.deltaTime: moveDirection.y -= gravity * Time.deltaTime;

Line 67 you're then adjusting moveDirection again by Time.deltaTime: controller.Move(Camera.main.transform.TransformDirection(moveDirection) * Time.deltaTime);

And then finally you're calling the Movement function from FixedUpdate (which runs at a regular number of steps per second anyway)

Also not quite sure what units you're trying to measure things in... gravity is 60 what? If you think in terms of standard unity like m/s or m/s2, you'll probably spot these kind of things easier.

Comment

People who like this

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

CharacterController Physics 1 Answer

How do i achieve a rounded jump arc? 1 Answer

Character won't jump on collider edge. 1 Answer

On a VR headset, the player jumps too many times sometimes when pressing a controller button. 1 Answer

Advice needed on my first c# 2D jump script 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