So I currently drafted up this code to make a simple sprite bounce, now I am a complete begineer when it comes to code and I’d like to understand how to accomplish. Currently the ball bounces on screen tap but I want it to bounce on ball tap, and the ball should bounce in random directions only within the parameters of the screen.
- The ball to only bounce specifically when it is tapped
- The ball to bounce in random directions within the screen(forward, left, right)
Here is a video Link as to what my code below does: 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallBounce : MonoBehaviour {
Vector3 velocity = Vector3.zero;
public Vector3 gravity;
public Vector3 bounceVelocity;
public float maxSpeed = 5f;
bool didBounce = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) {
didBounce = true;
}
}
void FixedUpdate () {
velocity += gravity * Time.deltaTime;
if (didBounce == true) {
didBounce = false;
velocity += bounceVelocity;
}
velocity = Vector3.ClampMagnitude (velocity, maxSpeed);
transform.position += velocity * Time.deltaTime;
}
}