Rigidbody Jumping issues
Hey guys!
I've been programming a game for quite some time now, have had it released a few times, including on the Wii U and I, as well as my players have been running into a random issue. In the game you control a speeding ship, you are able to control the throttle and movement while jumping onto platforms.
The problem is, at times my ship doesn't always jump. It seems to be when the ship is nearing the end of a platform, when you hit the jump button the ship doesn't jump, causing the player to die.
Here's my code, keep in mind please, I have taught myself coding and realize there is nothing fancy here, in fact it's quite messy code. But if you have any idea, or suggestions please do let me know!
I've been working on it for the past few days and as you can see I've added multiple redundancies to force grounding as I assumed it was an issue with the ship not detecting it was on the ground properly... anyways, any suggestions would be great!
#pragma strict
var jump : float = 55;
var secondsLeft : float = 0.5;
var Speed : float = 0;//Don't touch this
var ForwardSpeed : float = 0;//Don't touch this
var MaxSpeed : float;//This is the maximum speed that the object will achieve
var Acceleration : float = 10;//How fast will object reach a maximum speed
var Deceleration : float;//How fast will object reach a speed of 0
var score : float;
var isGrounded : boolean = true;
var Explosion : GameObject;
var Splash : GameObject;
var Ship : Transform;
var BumpSound : AudioClip;
var SplashSound : AudioClip;
var EngineSound :AudioClip;
var CollectSound : AudioClip;
var yourSpeed = 0;
var yourMaxSpeed = MaxSpeed;
//var rate : float;
var customSkin : GUISkin;
var Jumpomatic : String;
var myPosition : Vector3;
var LJoyPos : float;
var RJoyPos : float;
var gameObjects : GameObject[];
static var collectedGas : boolean = false;
var gravo : String;
public var amount : float = 50f;
var distToGround : float;
function OnGUI ()
{
GUI.skin = customSkin;
var kph = ForwardSpeed;
GUI.Label(Rect (600, 20, 500, 25), kph.ToString("F0") + " MPH");
//GUI.Label(Rect (600, 100, 500, 25), RJoyPos.ToString("F3"));
//GUI.Label(Rect (600, 25, 200, 25), "Speedometer");
GUI.Label(Rect (1050, 50, 200, 25), "Jump-o-matic");
GUI.Label(Rect (1100, 20, 200, 25), Jumpomatic);
GUI.Label(Rect (800, 50, 350, 25), "Gravo-Meter");
GUI.Label(Rect (800, 20, 350, 25), gravo);
//GUI.Label(Rect (350, 20, 500, 25), score.ToString("F0"));
//GUI.HorizontalScrollbar(Rect (595,10,200,25), 0, yourSpeed,0, yourMaxSpeed);
}
function OnTriggerEnter (other : Collider)
{
//check if it's the player that is colliding with this reset object by name/tag or anything else you want
if (other.name == "Change Song")
{
GetComponent.<AudioSource>().clip = SplashSound;
GetComponent.<AudioSource>().Play();
}
if (other.tag == "Collectible") {
score += 1;
GetComponent.<AudioSource>().clip = CollectSound;
GetComponent.<AudioSource>().Play();
PlayerPrefs.SetInt("Score", score);
Destroy(other.gameObject);
}
}
function Start () {
score = (PlayerPrefs.GetInt("Score"));
distToGround = GetComponent.<Collider>().bounds.extents.y;
isGrounded = true;
}
function OnCollisionStay(collision : Collision)
{
if(collision.gameObject.layer == LayerMask.NameToLayer("Platform"))
{
isGrounded = true;
//audio.PlayOneShot(BumpSound);
}
if(collision.gameObject.layer == LayerMask.NameToLayer("Gas"))
{
isGrounded = true;
//audio.PlayOneShot(BumpSound);
}
}
function OnCollisionExit(collision : Collision)
{
if(collision.gameObject.layer == LayerMask.NameToLayer("Platform"))
{
isGrounded = false;
//audio.PlayOneShot(BumpSound);
}
if(collision.gameObject.layer == LayerMask.NameToLayer("Gas"))
{
isGrounded = false;
//audio.PlayOneShot(BumpSound);
}
}
function OnCollisionEnter(collision : Collision)
{
if(collision.gameObject.layer == LayerMask.NameToLayer("Gas"))
{
var rigidBody = GetComponent.<Rigidbody>();
rigidBody.isKinematic = true;
isGrounded = true;
//audio.PlayOneShot(BumpSound);
} else if (!CheckGrounding) {
// we must have hit something in mid-air!
Debug.Log("Ooof!");
}
if(collision.gameObject.layer == LayerMask.NameToLayer("Platform"))
{
//var rigidBody = GetComponent.<Rigidbody>();
rigidBody.isKinematic = true;
isGrounded = true;
//audio.PlayOneShot(BumpSound);
}
if((collision.gameObject.layer == LayerMask.NameToLayer("Platform"))&&(CheckGrounding))
{
isGrounded = true;
CheckGrounding();//audio.PlayOneShot(BumpSound);
}
if(collision.gameObject.name == "Death")
{
score = 0;
PlayerPrefs.SetInt("Score", score);
myPosition = transform.position;
Instantiate(Explosion, new Vector3(myPosition.x,myPosition.y, myPosition.z), Quaternion.identity);
Destroy (gameObject);
}
if(collision.gameObject.layer == LayerMask.NameToLayer("Secret"))
{
}
if(collision.gameObject.layer == LayerMask.NameToLayer("Water"))
{
GetComponent.<AudioSource>().PlayOneShot(SplashSound);
myPosition = transform.position;
Instantiate(Splash, new Vector3(myPosition.x,myPosition.y, myPosition.z), Quaternion.identity);
Destroy (gameObject);
}
if(collision.gameObject.layer == LayerMask.NameToLayer("Gas"))
{
collectedGas = true;
}
if(collision.gameObject.layer == LayerMask.NameToLayer("Bounce"))
{
isGrounded = false;
GetComponent.<Rigidbody>().AddForce(Vector3.up * 3500);
}
if(collision.gameObject.layer == LayerMask.NameToLayer("Slow"))
{
isGrounded = true;
if (ForwardSpeed > 0) {
ForwardSpeed = ForwardSpeed - Acceleration * 500 * Time.deltaTime;
}
if (ForwardSpeed < 0) {
ForwardSpeed = 0;
}}
}
function Update () {
distToGround = GetComponent.<Collider>().bounds.extents.y;
CheckGrounding();
//RJoyPos = Input.GetAxis ("Horizontal");
//LJoyPos = Input.GetAxis ("RStick Up/Down");
yourSpeed = ForwardSpeed;
var fireRate : float = .5;
var nextFire : float = 2;
var h : float = Input.GetAxis("Horizontal") * amount * Time.deltaTime;
var v : float = Input.GetAxis("Vertical") * amount * Time.deltaTime;
if ((Input.GetAxis("Horizontal")<0)&&(Speed < MaxSpeed))
Speed = Speed - Acceleration * Time.deltaTime;
else if ((Input.GetAxis("Horizontal")>0)&&(Speed > -MaxSpeed))
Speed = Speed + Acceleration * Time.deltaTime;
else
{
if(Speed > Deceleration * Time.deltaTime)
Speed = Speed - Deceleration * Time.deltaTime;
else if(Speed < -Deceleration * Time.deltaTime)
Speed = Speed + Deceleration * Time.deltaTime;
else
Speed = 0;
}
transform.position.x = transform.position.x + Speed * Time.deltaTime;
if ((Input.GetAxis("Vertical")<0)&&(ForwardSpeed > 0))
{
ForwardSpeed = ForwardSpeed - Acceleration * 4 * Time.deltaTime;
}
else if ((Input.GetAxis("Vertical")<0)&&(ForwardSpeed <= 0)){
ForwardSpeed = 0;
}
if (Input.GetAxis("Vertical")>0)
if (ForwardSpeed <= 150) {
ForwardSpeed = ForwardSpeed + Acceleration * Time.deltaTime;
}
else
{
if(ForwardSpeed > Deceleration * Time.deltaTime)
ForwardSpeed = ForwardSpeed - Deceleration * Time.deltaTime;
else if (ForwardSpeed < -Deceleration * Time.deltaTime)
ForwardSpeed = ForwardSpeed + Deceleration * Time.deltaTime;
else
ForwardSpeed = 0;
}
if (Application.loadedLevel == 19) {
ForwardSpeed = 150;
}
if (Application.loadedLevel == 20) {
ForwardSpeed = 150;
}
transform.position.z = transform.position.z + ForwardSpeed/1.5 * Time.deltaTime /2;
if (Input.GetAxis("Escape")) {
Level_Select.levelToLoad = 3;
Application.LoadLevel(23);
// Application.LoadLevel(3);
}
}
function FixedUpdate()
{
Debug.Log(Input.inputString);
var hit = new RaycastHit();
var dist : float = .5;
var dir : Vector3;
dir = Vector3(0,-1,0);
if (Physics.Raycast(transform.position,dir,hit,dist))
{
if (hit.collider.gameObject.tag == "Platform"){
isGrounded = true;
}
if (hit.collider.gameObject.tag == "Gas"){
isGrounded = true;
}
if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Water"))
{
GetComponent.<AudioSource>().PlayOneShot(SplashSound);
myPosition = transform.position;
Instantiate(Splash, new Vector3(myPosition.x,myPosition.y, myPosition.z), Quaternion.identity);
Destroy (gameObject);
}
}
if (Input.GetAxis("Jump"))
{
var rigidBody = GetComponent.<Rigidbody>();
CheckGrounding();
if (Physics.Raycast(transform.position,dir,hit,dist))
{
if (rigidBody.velocity.y != 0)
{
GetComponent.<Rigidbody>().velocity.y = 0;
}
if (hit.collider.gameObject.tag == "Platform"){
GetComponent.<AudioSource>().PlayOneShot(BumpSound);
GetComponent.<Rigidbody>().AddForce(new Vector3(0, jump,0), ForceMode.Impulse);
isGrounded = false;
//Debug.Log("Player is on " + hit.collider.gameObject.name);
}
if (hit.collider.gameObject.tag == "Gas"){
GetComponent.<AudioSource>().PlayOneShot(BumpSound);
GetComponent.<Rigidbody>().AddForce(new Vector3(0, jump,0), ForceMode.Impulse);
isGrounded = false;
//Debug.Log("Player is on " + hit.collider.gameObject.name);
}
}
}
if (!CheckGrounding)
{
//isGrounded = false;
}
else
{
// isGrounded = true;
// Jumpomatic = "Ready";
}
if (isGrounded == false)
{
Jumpomatic = "Active";
}
else{
Jumpomatic = "Ready";
}
}
function CheckGrounding() : boolean
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
//Debug.Log(CheckGrounding());
}
Your answer
Follow this Question
Related Questions
Unity skipping input 0 Answers
Why is my player getting stuck when jumping into awalls? 0 Answers
please help me with my jumping script 1 Answer