- Home /
character controller wont rise when I Jump :(
Good news, I've finally got my jump animation working, bad news is I cant jump over obstacles because my character controller wont move when I jump here's an example,
also if i let go of the directional buttons my character automatically returns to facing forwards whether running or jumping, i'd like to be able to fix that too :)
#pragma strict
function Start () {
animation["Jump_pose"].layer = 2;
}
function Update () {
if (Input.GetButtonDown ("Jump"))
animation.CrossFade ("Jump_pose");
}
This is my Jump script, above and my character controller script below.
#pragma strict
var rotationSpeed : float = 10;
var walkSpeed : float = 7;
var gravity : float = 50;
private var yRot : float;
var body : Transform;
function Update () {
var Controller : CharacterController = GetComponent(CharacterController);
var vertical : Vector3 = transform.TransformDirection(Vector3.forward);
var horizontal : Vector3 = transform.TransformDirection(Vector3.right);
var height : Vector3 = transform.TransformDirection(Vector3.up);
if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")){
animation.CrossFade("walk", 0.2);
animation["walk"].speed = walkSpeed/10;
Controller.Move((vertical *(walkSpeed* Input.GetAxis("Vertical"))) *Time.deltaTime);
Controller.Move((horizontal *(walkSpeed* Input.GetAxis("Horizontal"))) *Time.deltaTime);
}else{
animation.CrossFade("idle", 0.2);
}
if(Input.GetAxis("Mouse X")){
yRot += 10 * Input.GetAxis("Mouse X");
}
Controller.Move(height *-gravity* Time.deltaTime);
}
function LateUpdate(){
if(Input.GetAxis("Vertical") == 0){
if(Input.GetAxis("Horizontal") > 0){
body.localEulerAngles.y = 90;
}else if(Input.GetAxis("Horizontal") < 0){
body.localEulerAngles.y = -90;
}
}else if(Input.GetAxis("Vertical") > 0){
if(Input.GetAxis("Horizontal") > 0){
}else if(Input.GetAxis("Horizontal") < 0){
}
}else if(Input.GetAxis("Vertical") < 0){
if(Input.GetAxis("Horizontal") > 0){
}else if(Input.GetAxis("Horizontal") < 0){
}
}
}
hope someone can help! x
Answer by Kachupi · Feb 13, 2014 at 12:22 PM
never mind i found the problem, i used the script here http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html and stuck my animation cross fade in it, i also realised i had to take any gravity effecting script off of my character movement script so my separate jump script dealt with all the gravity uses. heres my script now,
pragma strict
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Start () {
animation["jump"].layer = 2;
}
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
animation.CrossFade ("jump");
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
also I had to play around with the gravity level in the inspector so it fitted my jump animation, i'm gonna leave this on here for people to find who have had similar issues/problems, hope this helps! x
Your answer
Follow this Question
Related Questions
Jumping not always work 2 Answers
Player sticks to ground occasionally after trying to jump 0 Answers
Problems with jumping script 1 Answer
Unity Character Controller jumps instantly 0 Answers
Detecting CharacterController jumping 0 Answers