I am doing an infinite runner, the idea is to have a ball that is constantly rolling using AddRelativeTorque but I’m having a problem when it collides into the platforms. It kills the momentum and it needs to be built up again.
See the picture below, my ball has just hit the platform above it and now wont make it to the next platform, resulting in game over.
Is there a way I can hit the platform without losing the speed of the ball?
Here is my code:
using UnityEngine;
using System.Collections;
public class platform : MonoBehaviour
{
public Rigidbody target;
public float speed = 5f;
public int jumpHeight = 6;
public bool isFalling = false;
private bool isJumping = false;
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.R))
{
Application.LoadLevel(0);
}
if(Input.GetButtonDown("Fire1") && isFalling == false)
isJumping = true;
}
// Update is called once per frame
void FixedUpdate ()
{
ballMovements ();
}
void ballMovements()
{
rigidbody.AddRelativeTorque(Vector3.right * -1 * speed);
//transform.Translate(1f * Time.deltaTime, 0f, 0f);
if( isJumping )
{
isJumping = false;
Debug.Log ("clicked");
Vector3 temp = rigidbody.velocity;
temp.y = jumpHeight;
rigidbody.velocity = temp;
}
isFalling = true;
}
void OnCollisionStay()
{
isFalling = false;
}
}