- Home /
Change Movespeed based on Mouse Input
Basically have a vertical 2D platformer and the code will hopefully better explain what I am trying to do. However what I want is if I click on a positive X position, I want positive movespeed, if negative, then negative.
I currently have this implemented, but only the first if statement works. My Platform generator has a similar set up with If/Else if statements, so I am curious why this doesn't work?
void Update () {
grounded = Physics2D.IsTouchingLayers (myCollider, whatIsGround);
if(Input.GetMouseButtonDown (0))
{
if (grounded) {
if (Input.mousePosition.x > 0) {
myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y);
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
Debug.Log ("option 1");
}
else if (Input.mousePosition.x < 0) {
myRigidbody.velocity = new Vector2 (-moveSpeed, myRigidbody.velocity.y);
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
Debug.Log ("option 2");
}
}
}
}
I also think that movespeed should be adjusted based on distance, but that is a later issue I will tackle.
Hopefully that made some sense! Cheers to anyone who helps.
try having both statements be if, and not if and else if
Your answer
![](https://koobas.hobune.stream/wayback/20220612114530im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Do something if position Y is 4. (C#) 2 Answers
How does instantiate connect to rigidbody?,How does instantiate userigidbody without getcomponent? 1 Answer
How can I pragmatically move a navmesh rigid body when changing scenes? 0 Answers
How do I Open/Close my option menu with one key C# 1 Answer
Help with if statement 1 Answer