Question by
tiyyargee · Feb 15, 2016 at 01:40 PM ·
c#script.animator controllerjumping
how i can jump?
hello guys , really i'm suffering with jump animation. i set my animator correctly and i made my script , when i press space the player plays the jump animation but he is not raising from the ground ,he is playing it but still grounded , please help me to make my player raise up here is my script; using UnityEngine; using System.Collections;
public class playerscript : MonoBehaviour{
public Quaternion newrotation;
public float smooth = 0.05f;
public Transform cam;
public CharacterController controlleer;
public float speed;
public float jmpheight;
public static bool isattacking;
public static bool die;
public static bool isrunning;
Rigidbody rigid;
public static bool ismoving;
Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
rigid = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
float v = Input.GetAxis ("Vertical");
float h = Input.GetAxis ("Horizontal");
movement(v,h);
Running (h,v);
}
void movement (float v, float h) {
if (!isattacking&&!die)
{
if (h != 0f || v != 0f)
{
ismoving = true;
rotate (v, h);
controlleer.SimpleMove (transform.forward * speed);
if(!isrunning)
{
anim.SetFloat("speed",1);
}
}
else
{
isattacking = false;
anim.SetFloat ("speed",0);
}
}
// make the player run
}
void Running(float h,float v)
{
if ((Input.GetKeyDown (KeyCode.LeftShift)) && (h != 0 || v != 0))
{
isattacking = false;
isrunning = true;
speed = speed * 2f;
anim.SetBool ("run",true);
rotate (h, v);
controlleer.SimpleMove (transform.forward * speed);
}
else if (Input.GetKeyUp (KeyCode.LeftShift))
{
isrunning= false;
anim.SetBool ("run", false);
speed = speed / 2;
}
/// jumping
///
///
if (Input.GetKeyDown (KeyCode.Space) && isrunning)
{
anim.SetBool ("jmp", true);
}
else if (Input.GetKeyUp (KeyCode.Space) )
{
anim.SetBool ("jmp", false);
}
}
Comment
You never change the position of the object. Try something like: controlleer.Simple$$anonymous$$ove (transform.up * speed);
It whould be posted as an comment (using reply under other comment) not as an answer...
About the problem, @Salmjak is right, you are only moving forward, you need to move up too!
You can try somthing like that:
void Running(float h,float v)
{
if ((Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.LeftShift)) && (h != 0 || v != 0))
{
isattacking = false;
isrunning = true;
speed = speed * 2f;
anim.SetBool ("run",true);
rotate (h, v);
// $$anonymous$$OVED YOUR $$anonymous$$OVE$$anonymous$$ENT LINE ALL WAY DOWN, SO YOU WILL UPDATE EVERYTHING ONLY ONCE
}
else if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.LeftShift))
{
isrunning= false;
anim.SetBool ("run", false);
speed = speed / 2;
}
/// jumping
///
///
// THIS IS JUST FOR TEST PURPOSE
float jumpspeed;
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) && isrunning)
{
// SETTING THE JU$$anonymous$$P SPEED
jumpspeed = 50;
anim.SetBool ("jmp", true);
}
else if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Space) )
{
anim.SetBool ("jmp", false);
}
// AND HERE IS THE $$anonymous$$OVE$$anonymous$$ENT LINE
controlleer.Simple$$anonymous$$ove (transform.forward * speed + transform.up * jumpspeed);
}