Facebook
From Adrian Piecek, 7 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 248
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public class SphereController : MonoBehaviour
  6. {
  7.     int layer = 0;
  8.     Rigidbody rigidbody;
  9.  
  10.     void Start()
  11.     {
  12.         rigidbody = transform.GetComponent<Rigidbody>();
  13.     }
  14.  
  15.     void Update()
  16.     {
  17.         changeLayer();
  18.         changePosition();
  19.     }
  20.  
  21.     void changeLayer()
  22.     {
  23.             if (Input.GetKey(KeyCode.UpArrow))
  24.             {
  25.                 layer = 1;
  26.             }
  27.  
  28.             if (Input.GetKey(KeyCode.DownArrow))
  29.             {
  30.                 layer = 0;
  31.             }
  32.  
  33.             float delta = (layer * 2f - 2f) - rigidbody.position.z;
  34.  
  35.             Vector3 velocity = rigidbody.velocity;
  36.             velocity.z = delta * 3f;
  37.             rigidbody.velocity = velocity;
  38.         }
  39.     }
  40.  
  41.     void changePosition()
  42.     {
  43.  
  44.             Vector3 direction = Vector3.zero;
  45.  
  46.             if (Input.GetKey(KeyCode.LeftArrow))
  47.             {
  48.                 direction = -Vector3.forward;
  49.             }
  50.  
  51.             if (Input.GetKey(KeyCode.RightArrow))
  52.             {
  53.                 direction = Vector3.forward;
  54.             }
  55.  
  56.             rigidbody.AddTorque(direction * 25f);
  57.     }
  58.  
  59.  
  60.