Advertisement
Guest User

Untitled

a guest
Jul 11th, 2019
109
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.  
  5. public class Rocket : MonoBehaviour
  6. {
  7.     Rigidbody rigidBody;
  8.     AudioSource audioSource;
  9.  
  10.     bool thrustersPlay;
  11.  
  12.  
  13.     // Start is called before the first frame update
  14.     void Start()
  15.     {
  16.         audioSource = GetComponent<AudioSource>();
  17.         rigidBody = GetComponent<Rigidbody>();
  18.         thrustersPlay = false;
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.         Thrusters();
  25.         Rotation();
  26.     }
  27.  
  28.     void Thrusters()
  29.     {
  30.         if (Input.GetKey(KeyCode.Space)) //Can thrust while rotating
  31.         {
  32.             print("Thrusting");
  33.             rigidBody.AddRelativeForce(Vector3.up);
  34.             if (!audioSource.isPlaying) //plays only once
  35.             {
  36.                 audioSource.Play();
  37.             }
  38.  
  39.         }
  40.         else
  41.         {
  42.             audioSource.Stop();
  43.         }
  44.     }
  45.  
  46.     void Rotation()
  47.     {
  48.  
  49.         rigidBody.freezeRotation = true; //take manual control of rotation
  50.         if (Input.GetKey(KeyCode.A)) // Rotating left
  51.         {
  52.             transform.Rotate(Vector3.forward);
  53.         }
  54.         else if (Input.GetKey(KeyCode.D)) //Rotating right
  55.         {
  56.             transform.Rotate(-Vector3.forward);
  57.         }
  58.         rigidBody.freezeRotation = false; //resume physics control of rotation
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement