- Home /
While Moving Left or Right my character falls more slowly.
Here's my script and also I should mention upon letting go of left or right buttons fall speed greatly increases. Also while your here I did use the unity 2d platformer tutorial to check if grounded and if grounded is not working.
public GameObject MarioMobster;
public Vector3 RightForce = new Vector3(404, 0);
public Vector3 leftforce = new Vector3(-404, 0);
public Vector3 Jumpforce = new Vector2 (0, 404);
public static int health = 3;
public Sprite sprite1;
public Sprite sprite2;
private SpriteRenderer spriterenderer;
//public GameObject bullets;
private Transform groundcheck;
private bool grounded = false;
private bool LookingRighty = true;
private bool LookingLefty = false;
public float bulletspeed = 20f;
public Rigidbody2D bullets;
// Use this for initialization
void Start () {
spriterenderer = GetComponent<SpriteRenderer> ();
if (spriterenderer.sprite == null) {
spriterenderer.sprite = sprite1;
}
}
// Update is called once per frame
void Update () {
grounded = Physics2D.Linecast(transform.position, groundcheck.position, 1 << LayerMask.NameToLayer("Ground"));
}
void Awake(){
groundcheck = transform.Find ("groundcheck");
}
public void Right (){
GetComponent<Rigidbody2D>().velocity = RightForce;
//GetComponent<Rigidbody2D>().AddForce(RightForce);
if (LookingLefty = true) {
LookingLefty = false;
LookingRighty = true;
}
Debug.Log ("moveScript.Right was called succesfully");
if(spriterenderer.sprite == sprite2){
spriterenderer.sprite = sprite1;
}
}
public void Left(){
GetComponent<Rigidbody2D> ().velocity = leftforce ;
Debug.Log ("left has also been called");
if (LookingRighty = true) {
LookingRighty = false;
LookingLefty = true;
}
if(spriterenderer.sprite == sprite1){
spriterenderer.sprite = sprite2;
}
}
public void Jump(){
Debug.Log ("jump has also been called");
if (grounded) {
GetComponent<Rigidbody2D> ().velocity = Jumpforce;
Debug.Log("grounded is true");
}
}
}
Why are rightForce and leftForce Vector3s? $$anonymous$$ake them Vector2s and try it again
Answer by 3DDean · Jul 30, 2015 at 03:31 AM
The reason the character is falling more slowly is because you are setting the y velocity to zero. So the physics update pulls it down. But every frame you set the velocity to zero. So to fix it maybe try using add force instead of setting the velocity directly. That should fix it. Then you said that checking if you are grounded is not working? Is unity giving any errors?
Answer by TheHeron · Jul 30, 2015 at 12:31 PM
Looking at your code, I assume you removed AddForce
because it was repeatedly adding force as long as you were holding right or left.
What you most likely want to do is instead use something like this:
[...] .velocity = new Vector3(RightForce.x,OurRigidBody2D.velocity.Y);
This way you maintain your user's current vertical velocity as per the physics system while keeping horizontal movement.
Also, as a quick aside that might help you, you don't want to be calling GetComponent
every time your user moves right or left and such - it's a rather slow operation. Consider instead using the Awake()
or Start()
methods built into MonoBehaviour
to store a permanent reference to your GameObject's RigidBody2D. You've already done this with your SpriteRenderer
, the process is essentially identical. Then when you're using it in the Right/Left/Jump parts of your code, you'd instead use OurRigidBody2D.velocity
or whatever you called your reference to your RigidBody2D.
Your answer
Follow this Question
Related Questions
Unity 2D - Rigidbody2D Velocity sudden freeze 0 Answers
Multiple Cars not working 1 Answer
Boss Health Bar 1 Answer
Photon position syncing 0 Answers
Physics2D checking if object goes out of OverlapArea 1 Answer