- Home /
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;
}
}
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.
Your answer
Follow this Question
Related Questions
CharacterController Physics 1 Answer
How do i achieve a rounded jump arc? 1 Answer
Jumping while moving 1 Answer
FPS Controller Duck 2 Answers