Advertisement
CakeMeister

3dBall Player joystick

Aug 8th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Player : MonoBehaviour {
  7.  
  8.  
  9.     public Rigidbody rid;
  10.     public Vector3 direction;
  11.     public float speed = 500f;
  12.     public int points = 0;
  13.  
  14.     public Text wintext;
  15.     public Text scoretext;
  16.  
  17.     public Joystick movejoystick;
  18.     // Use this for initialization
  19.     void Start () {
  20.         rid = GetComponent<Rigidbody>();
  21.         scoretext.text = "Score: " + points.ToString();
  22.         wintext.text = "";
  23.     }
  24.    
  25.    
  26.     void Update () {
  27.         float h = Input.GetAxis("Horizontal");
  28.         float v = Input.GetAxis("Vertical");
  29.  
  30.         direction.x = h;
  31.         direction.z = v;
  32.  
  33.         if (movejoystick.direction != Vector3.zero)
  34.         {
  35.             direction = movejoystick.direction;
  36.         }
  37.         rid.AddForce(direction.normalized * speed * Time.deltaTime);
  38.  
  39.        
  40.         if (points >= 4)
  41.             wintext.text = "WIN!";
  42.        
  43.     }
  44.  
  45.     private void FixedUpdate()
  46.     {
  47.         rid.velocity = new Vector3(rid.velocity.x * 0.95f, rid.velocity.y, rid.velocity.z * 0.95f);
  48.     }
  49.     private void OnTriggerEnter(Collider other)
  50.     {
  51.         if (other.CompareTag("cube"))
  52.         {
  53.             points += 1;
  54.             other.gameObject.SetActive(false);
  55.             scoretext.text = "Score: " + points.ToString();
  56.         }
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement