Skip to content

Instantly share code, notes, and snippets.

Created January 25, 2017 15:27
Show Gist options
  • Save anonymous/10b9f4e9485641a3ad25ebb5f7eef2ab to your computer and use it in GitHub Desktop.
Save anonymous/10b9f4e9485641a3ad25ebb5f7eef2ab to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BallController : MonoBehaviour {
public float velocity = 1f;
public GameObject particle;
public Text scoreText;
public GameObject ground;
public Button button;
private Rigidbody rb;
private int score;
private float x, y;
void Start () {
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate () {
Vector3 moviment;
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
var touchDeltaPosition = Input.GetTouch (0).deltaPosition;
x = Mathf.Min (Mathf.Max (-1.2f, touchDeltaPosition.x), 1.2f);
y = Mathf.Min (Mathf.Max (-1.2f, touchDeltaPosition.y), 1.2f);
moviment = new Vector3 (x * velocity, 0, y * velocity);
} else {
moviment = new Vector3 (Input.GetAxis ("Horizontal") * velocity, 0, Input.GetAxis ("Vertical") * velocity);
}
rb.AddForce (moviment);
}
void OnTriggerEnter(Collider other) {
DestroyObstacle (other);
UpdateScore ();
}
void DestroyObstacle(Collider obstacle) {
Instantiate (particle, obstacle.transform.position, Quaternion.identity);
Destroy (obstacle.gameObject);
}
void UpdateScore () {
score++;
scoreText.text = "Score: " + score.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment