- Home /
Help with adding jump function to player.
I added or tried to add a jump function similar to how I added the move function. I also tried it the same way I added crouch/attack function. It doesn't do anything though he just sits still. Sometimes I can get it to work, but if I tap the button he barely moves up and the animation doesn't continue. I tried GetButtonUp but that doesn't work either.
Lines with asterisk symbols is what I tried to do to add jumping.
heres the code with jump function in it:
public float speed = 1.0f;
*****public float jumpspeed = 1.0f;******
private Transform thisTransform;
private Sprite sprite;
private bool flipped;
// Use this for initialization
void Awake () {
thisTransform = transform;
sprite = (Sprite)GetComponent(typeof(Sprite));
}
// Update is called once per frame
void Update () {
Vector3 moveVector = new Vector3(0,0,0);
bool moving = false;
************bool jumping = false;*************
*********if(Input.GetKey(KeyCode.Space)){
moveVector.y += 2.0f;
jumping = false;
}*************
if(Input.GetKey(KeyCode.A)){
moveVector.x -= 1.0f;
moving = true;
}
if(Input.GetKey(KeyCode.D)){
moveVector.x += 1.0f;
moving = true;
}
if(Input.GetKey(KeyCode.Mouse0)){
Attack();
} else {
if(!IsAttacking()) {
if(moving) {
Move(moveVector);
} else {
Stand();
}
}
if(Input.GetKey(KeyCode.S)){
Crouch();
} else {
if(!IsCrouching()) {
}
if(moving) {
Move(moveVector);
}
else {
Stand();
}
}
}
}
private void Stand() {
sprite.Stop();
}
private void Move(Vector3 moveVector) {
FlipSprite(moveVector.x);
moveVector *= speed * Time.deltaTime;
thisTransform.position = thisTransform.position + moveVector;
sprite.Play("walk");
}
private void FlipSprite(float x) {
if(x == 0.0f) return;
float xDir = Mathf.Sign(x);
if(!flipped && xDir < 0.0f) {
flipped = true;
sprite.SetFlippedState(true, false);
} else if(flipped&& xDir > 0.0f) {
flipped = false;
sprite.SetFlippedState(false, false);
}
}
private void Attack() {
if(IsAttacking()) return;
sprite.Play("Sword");
}
private bool IsAttacking() {
if(sprite.IsAnimationPlaying("Sword")){ return false;}
return false;
}
private void Crouch() {
if(IsCrouching()) return;
sprite.Play("crouch");
}
private bool IsCrouching() {
if(sprite.IsAnimationPlaying("crouch")) {return false;}
return false;
}
************private void Jump(Vector3 moveVector) {
FlipSprite(moveVector.x);
moveVector *= jumpspeed * Time.deltaTime;
thisTransform.position = thisTransform.position + moveVector;
sprite.Play("jump");****************
}
}
Answer by getyour411 · Sep 06, 2013 at 09:19 PM
I don't see you calling Jump() anywhere. Somewhere tween 45 - 65 you are probably falling through to Stand(), Google 'fence the wolf' and use some print/debugs to see where your code is failing.
I tried what you said and called Jump(),but it still doesn't work. It gives me the error that Jump calls for 0 arguments. I tried it a different way that allowed me to call jump, but that made my walking animation not work, I couldn't attack and I couldn't crouch.
This is the newer script:
using UnityEngine;
using System.Collections;
using SpriteFactory;
public class player : $$anonymous$$onoBehaviour {
public float speed = 1.0f;
public float jumpspeed = 1.0f;
private Transform thisTransform;
private Sprite sprite;
private bool flipped;
// Use this for initialization
void Awake () {
thisTransform = transform;
sprite = (Sprite)GetComponent(typeof(Sprite));
}
// Update is called once per frame
void Update () {
Vector3 moveVector = new Vector3(0,0,0);
bool moving = false;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space)){
Jump();
} else {
if(!IsJumping()) {
if(moving) {
$$anonymous$$ove(moveVector);
} else {
Stand();
}
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)){
moveVector.x -= 1.0f;
moving = true;
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)){
moveVector.x += 1.0f;
moving = true;
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0)){
Attack();
} else {
if(!IsAttacking()) {
if(moving) {
$$anonymous$$ove(moveVector);
} else {
Stand();
}
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)){
Crouch();
} else {
if(!IsCrouching()) {
}
if(moving) {
$$anonymous$$ove(moveVector);
}
else {
Stand();
}
}
}
}
}
private void Stand() {
sprite.Stop();
}
private void $$anonymous$$ove(Vector3 moveVector) {
FlipSprite(moveVector.x);
moveVector *= speed * Time.deltaTime;
thisTransform.position = thisTransform.position + moveVector;
sprite.Play("walk");
}
private void FlipSprite(float x) {
if(x == 0.0f) return;
float xDir = $$anonymous$$athf.Sign(x);
if(!flipped && xDir < 0.0f) {
flipped = true;
sprite.SetFlippedState(true, false);
} else if(flipped&& xDir > 0.0f) {
flipped = false;
sprite.SetFlippedState(false, false);
}
}
private void Attack() {
}
private bool IsAttacking() {
if(sprite.IsAnimationPlaying("Sword")){ return false;}
return false;
}
private void Crouch() {
if(IsCrouching()) return;
sprite.Play("crouch");
}
private bool IsCrouching() {
if(sprite.IsAnimationPlaying("crouch")) {return false;}
return false;
}
private void Jump() {
if(IsJumping()) return;
sprite.Play("jump");
}
private bool IsJumping() {
if(sprite.IsAnimationPlaying("jump")) {return false;}
return false;
}
}
Answer by nasby321 · Sep 10, 2013 at 02:44 AM
I fixed it myself heres the new script if any one wants to see it:
using UnityEngine; using System.Collections; using SpriteFactory;
public class player : MonoBehaviour {
public float speed = 1.0f;
public float jumpSpeed = 10.0f;
public float gravity = 20.0f;
public bool grounded = true;
private Transform thisTransform;
private Sprite sprite;
private bool flipped;
// Use this for initialization
void Awake () {
thisTransform = transform;
sprite = (Sprite)GetComponent(typeof(Sprite));
}
// Update is called once per frame
void Update () {
Vector3 moveVector = new Vector3(0,0,0);
bool moving = false;
moveVector.y -= gravity * Time.deltaTime;
if(Input.GetKeyDown(KeyCode.Space)){
Jump();
}
if(Input.GetKey(KeyCode.A)){
moveVector.x -= 1.0f;
moving = true;
}
if(Input.GetKey(KeyCode.D)){
moveVector.x += 1.0f;
moving = true;
}
if(Input.GetKey(KeyCode.Mouse0)){
Attack();
} else {
if(!IsAttacking()) {
if(moving) {
Move(moveVector);
} else {
Stand();
}
}
if(Input.GetKey(KeyCode.S)){
Crouch();
} else {
if(!IsCrouching()) {
}
if(moving) {
Move(moveVector);
}
else {
Stand();
}
}
}
}
private void Stand() {
sprite.Stop();
}
private void Move(Vector3 moveVector) {
FlipSprite(moveVector.x);
moveVector *= speed * Time.deltaTime;
thisTransform.position = thisTransform.position + moveVector;
sprite.Play("walk");
}
private void FlipSprite(float x) {
if(x == 0.0f) return;
float xDir = Mathf.Sign(x);
if(!flipped && xDir < 0.0f) {
flipped = true;
sprite.SetFlippedState(true, false);
} else if(flipped&& xDir > 0.0f) {
flipped = false;
sprite.SetFlippedState(false, false);
}
}
private void Attack() {
if(IsAttacking()) return;
sprite.Play("Sword");
}
private bool IsAttacking() {
if(sprite.IsAnimationPlaying("Sword")){ return false;}
return false;
}
private void Crouch() {
if(IsCrouching()) return;
sprite.Play("crouch");
}
private bool IsCrouching() {
if(sprite.IsAnimationPlaying("crouch")) {return false;}
return false;
}
void Jump(){ if(grounded == true) { rigidbody.AddForce(Vector3.up * jumpSpeed); grounded = false; } }
void OnCollisionEnter(Collision col){
if(col.gameObject.tag == "floor"){
grounded = true;
sprite.Play("jump");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613113717im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Gravity and Jumping for a Character Controller 1 Answer
Adding a jump feature help? 2 Answers
Particular "Jump" Script. 2 Answers