- Home /
Jumping spring (pass a rigidbody velocity value from another script)
Hello, I am writing a script for a jumping spring for a player that is supposed to make the player jump at different speeds for each spring. The issue is that I have two springs in the same scene and they always have the same speed value no matter how I change both.
the script is as following:
public class Spring : MonoBehaviour
{
public GameObject player;
private Rigidbody2D RB;
public Vector2 Speed;
private Animator anim;
private AudioSource audi;
private bool JumpPlayer;
private bool JumpOnce;
// Start is called before the first frame update
void Start()
{
RB = player.GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
audi = GetComponent<AudioSource>();
}
void Update()
{
CheckIfPushed();
if (JumpPlayer && !JumpOnce)
{
StartCoroutine("PushJump");
}
else if (!JumpPlayer)
{
JumpOnce = false;
StopCoroutine("PushJump");
anim.SetBool("Push", false);
}
}
// Update is called once per frame
IEnumerator PushJump()
{
JumpOnce = true;
yield return new WaitForSeconds(0.1f);
anim.SetBool("Push", true);
RB.velocity = new Vector2 (Speed.x , Speed.y);
audi.Play();
}
void CheckIfPushed()
{
if (GameBrain.isPushed)
{
JumpPlayer = true;
}
else if (!GameBrain.isPushed)
{
JumpPlayer = false;
}
}
}
@knobblez yes I have two spring objects and I put two different speed values for each of them, but they react as one, when I try to jump on one or another they respond to one-speed value only, not to mention they animate the push animation together when I jump on one of them only. Actually, I prefab-ed the spring object which makes sense why they behave the same but then I tried to unpack the two of them and they still behave the same.
I'm assu$$anonymous$$g they share an animator, and if that's the case, then I believe since they are sharing an animator, they share variables, such as anim.SetBool("Push", true);. You can just copy the animator and assign them both. I'm not certain this is the best way to do this but it should tell you what the problem is.
And you may want to use AddForce rather than directly changing the velocity. The docs say that's not recommended as it "can result in unrealistic behaviour - use AddForce instead".
Answer by knobblez · Jan 22 at 10:32 AM
So you have 2 springs, each have this Spring.cs on them. You're setting the Speed Vector3 for each to different values and what happens? Are they resetting on Play or is the different just not noticeable?
Answer by akyu01 · Jan 23 at 07:29 AM
@knobblez it seems like the reason why they react the same is because I am using a raycast static bool (isPushed) for my player to detect if they touch the spring layer which causes them to behave the same for some reason. I tried to exchange this with player collision instead which works fine now.
Your answer
Follow this Question
Related Questions
Constant Force causing my object to twist/veer off. 0 Answers
(RigidBody) Velocity at an Angle 0 Answers
Help sliding Rigidbody down slope 1 Answer
How to destroy a gameObject only when inside a rigidBody 1 Answer
addForce PLEASE HELP!!! 2 Answers