Hello,
I liked the Roll a ball tutorial you can find here : https://unity3d.com/fr/learn/tutorials/projects/roll-ball-tutorial
I wanted to work more on this project and decided to add levels, some effects…
And since yesterday, I want to add a jump script. So i found how to jump with tutorials. But the problem is I want the jump to consider the speed of the sphere. If i go fast, i’ll jump further. The problem at the moment is : if I move fast, stop pressing the arrows (but the ball is still rolling) and press the jump key, it’ll jump straight. If i keep pressing the arrow, it’s like the speed will start from 0 (and not the speed i had when the ball was rolling). I did a video on YouTube to show you :
(first jump is when i stop pressing the arrows, second is when i’m still pressing them). What can I do ?
Here is my code (i kept everything in the code, even if it’s not the jump script) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ControllingSphere : MonoBehaviour {
public float speed;
private int count = 0; // Number of cubes collected
public int counttotal = 99; // Number of cubes in the scene
public int goToSceneNumber = 99; // Go to this scene when all the cubes are collected
public bool onGround; // Sphere grounded or not
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
onGround = true;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene(goToSceneNumber - 1); // Retry the level when R is pressed
}
if (onGround == true) // If the sphere is grounded
{
if (Input.GetKeyDown(KeyCode.Space)) // And if the player press Space
{
rb.velocity = new Vector3(0f, 5f, 0f); // The sphere will jump
onGround = false; // The sphere can't jump if it's in the air
}
}
}
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed); // Controlling the character
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Ground")) // If the sphere is touching the floor (tagged Ground)
{
onGround = true; // The sphere can jump again
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pickups")) // If the sphere collect a cube
{
other.gameObject.SetActive(false); // This cube is disabled
count++;
}
else if (other.gameObject.CompareTag("Classical")) // If the sphere collect a classical cube
{
other.gameObject.SetActive(false); // This cube is disabled
transform.localScale += new Vector3(0.1F, 0.1F, 0.1F); // Make the sphere bigger
count++;
}
else if (other.gameObject.CompareTag("Smaller")) // If the sphere collect a smaller cube
{
other.gameObject.SetActive(false); // This cube is disabled
transform.localScale += new Vector3(-0.1F, -0.1F, -0.1F); // Make the sphere smaller
count++;
}
if (count == counttotal)
{
SceneManager.LoadScene(goToSceneNumber); //If the sphere collected all the cubes, we go to the next level
}
}
}