Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Rocket : MonoBehaviour {
- Rigidbody rb;
- AudioSource rocket;
- void Start () {
- rb = GetComponent<Rigidbody>();
- rocket = GetComponent<AudioSource>();
- }
- void Update()
- {
- Thrust();
- Rotate();
- }
- private void Rotate()
- {
- rb.freezeRotation = true; //take control of rotation
- if (Input.GetKey(KeyCode.A))
- {
- transform.Rotate(Vector3.forward);
- }
- else if (Input.GetKey(KeyCode.D))
- {
- transform.Rotate(Vector3.back);
- }
- rb.freezeRotation = false; //resume physics control
- }
- private void Thrust()
- {
- if (Input.GetKey(KeyCode.Space))
- {
- rb.AddRelativeForce(Vector3.up);
- if (!rocket.isPlaying)
- {
- rocket.Play();
- }
- }
- else if (Input.GetKeyUp(KeyCode.Space))
- {
- rocket.Stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement