Why is my momentum not carrying over to the X axis?
I'm making a 2D car game similar to rocket league and i'm running into issues with "boosting". Any force or velocity I add is quickly taken away on the X axis when i'm not holding the designated key and moves slow, but if I face the sprite upwards (Y axis) it boosts/adds force at ultrasonic speed. I want my momentum to carry and gravity to affect the boost like it does when just normally driving but right as I take my hand off the key it comes to a dead halt unless its in the air (then it falls like it should). Here is my entire player control script (started using unity/coding a few days ago).
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerCtrl : MonoBehaviour {
public ParticleSystem boost;
public float maxVelocity;
public float thrust;
public float speed;
public float jumpPower;
public Transform groundCheckPoint;
public float groundCheckRadius;
public float velocidade = 30;
public LayerMask groundLayer;
private bool grounded;
Rigidbody2D rb;
ParticleSystem[] childrenParticleSytems;
// Start is called before the first frame update
void Start()
{
rb= GetComponent<Rigidbody2D> ();
childrenParticleSytems = gameObject.GetComponentsInChildren< ParticleSystem >();
}
// Update is called once per frame
void Update()
{
grounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
if(Input.GetKeyDown("left shift")){
rb.AddRelativeForce(Vector2.right * thrust * Time.deltaTime);
}
}
void FixedUpdate (){
float move = Input.GetAxis ("Horizontal");
rb.velocity = new Vector2 (speed *move, rb.velocity.y);
if (Input.GetKey("up")){
rb.transform.Rotate(0,0, velocidade * Time.deltaTime);
}
if (Input.GetKey("down")){
rb.transform.Rotate(0,0, -velocidade * Time.deltaTime);
}
if (Input.GetKey("space") && grounded) {
rb.AddRelativeForce(Vector2.up * jumpPower);
}
if(Input.GetKey("left shift")){
rb.AddRelativeForce(Vector2.right * thrust);
boost.Emit(1);
}
}
}
Your answer
Follow this Question
Related Questions
Projectile in angry birds not working as expected 3 Answers
Strange behavior when applying forces. 0 Answers
Using rb.velocity causes low gravity. 2 Answers
Figuring out what the acceleration will be before applying force to RigidBody2D 0 Answers
How to rotate Bullets alongside Gravity 2D (No RigidBody) 0 Answers