JUMP LEFT script problem

Hello guys, I have a little problem on my game. The game is about a ball which should rise as you press, you can press the left side of the screen and jump UP+LEFT also you can press the right side and jump UP+RIGHT, if you stop tapping you will fall and die, the camera follows you as you rise up. The problem is that my player only jumps up without any tilting left or right, here is my script for jumping :
P.S. there are 2 big buttons who activates the jump functions.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
[RequireComponent(typeof(Rigidbody2D))]
public class TapController : MonoBehaviour {
   
    public GameObject Camera;
    public float tapForce;
    public float tiltSmooth;
    public Vector3 startPos;
    public GameObject player;
    GameManager game;
    public Rigidbody2D rigidbody;
    Quaternion downRotation;
    Quaternion forwardRotation;
   
    void Start()
    {
        rigidbody = GetComponent<Rigidbody2D>();
        downRotation = Quaternion.Euler(0, 0, 0);
        forwardRotation = Quaternion.Euler(0, 0, 35);
       
       
    }
    public void JUMPRIGHT()
    {
        rigidbody = GetComponent<Rigidbody2D>();
        rigidbody.velocity = new Vector3(100f,0f, 0f);
        rigidbody.AddForce(new Vector3(0, 10, 0) , ForceMode2D.Impulse);
        player.GetComponent<Animator>().enabled = false;
        player.GetComponent<Rigidbody2D>().gravityScale = 10f;
    }
    public void JUMPLEFT()
    {
        rigidbody = GetComponent<Rigidbody2D>();
        rigidbody.velocity = new Vector3(0f-20.5f, 0f);
        rigidbody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);
        player.GetComponent<Animator>().enabled = false;
        player.GetComponent<Rigidbody2D>().gravityScale = 10f;
    }
    void Update()
    {
    }
    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "Falling Item")
        {
         
        }
       
        if (col.gameObject.tag == "DeadZone")
        {
           Camera.GetComponent<GameManager>().OnPlayerDied();
        }
    }
   
}

I’d be leery about setting the velocity and using AddForce at the same time, but not sure if that’s it. Is there any other script that affects the movement of this rigidbody?

To do that, you need to add an X component to the force added to the body.

Also, if I may just point out that both JUMPRIGHT and JUMPLEFT are doing basically the same thing. Yet the code in each differs slightly- doesn’t seem to be a problem as is but you can see how a problem could easily arise in future. You could consider doing something like this :

    void Start()
    {
        myRigidBody = GetComponent<Rigidbody2D>();
        myPlayerAnimator = player.GetComponent<Animator>();
        myPlayerRigidBody = player.GetComponent<Rigidbody2D>();
    }

    public void Jump(bool right)
    {
        myRigidBody.velocity = myStartVel;
        myRigidBody.AddForce(new Vector3(right ? mySideForce : -mySideForce, 10, 0), ForceMode2D.Impulse);
        myPlayerAnimator.enabled = false;
        myPlayerRigidBody.gravityScale = 10f;
    }

    readonly float mySideForce = 5f;
    readonly Vector3 myStartVel = new Vector3(100f, 0f, 0f);
    Rigidbody2D myRigidBody;
    Animator myPlayerAnimator;
    Rigidbody2D myPlayerRigidBody;

Incidentally, I presume that you are setting the velocity to stabilise the object prior to adding the force to it? If so, why is the X component set to 100?

Furthermore, bear in mind that physics calc’s are done in FixedUpdate(). I’m not sure where you are calling the jump methods from, but if you are doing so in a frame based way (i.e. from Update()), you may get some strange results at times. You may want to consider only setting a flag in the jump method and then add the force, if the flag is set, in the FixedUpdate().

After changing my code and changing the button’s function to “Jump” it does nothing. It doesn’t jump each time I press the button, it doesn’t jump anytime…

Where do you call Jump from?

From a button !

You mean a UI button? You’ve assigned Jump as the onClick event?

Right

And you re-assigned it after you change the name and you’re also passing a value for the new true/false parameter that you’ve added?

I re-assigned if after changing the name but what do you mean by passing a value of the new true/false parameter? :slight_smile:

@Doug_B 's code included a bool parameter, so if you changed your code to match, then now you can’t simply call the function, you also have to pass either true or false as a parameter.

Another way you can fix it is to add definitions for JUMPRIGHT and JUMPLEFT as wrappers for jump, like this:

public void JUMPRIGHT()
{
   Jump(true);
}

public void JUMPLEFT()
{
   Jump(false);
}

Then you can just do your button thing the same as you were doing before.

1 Like

Still doesn’t work …

So you must have some sort of error in your code, or your object is not setup properly in the editor. Do you have any messages in the console?

I have no messages in the console :slight_smile: