OnCollisionEnter

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rocket : MonoBehaviour
{
[SerializeField]
float rcsThrust = 100f;
[SerializeField]
float mainThrust = 100f;
AudioSource audioSource;
Rigidbody rigidBody;
// Start is called before the first frame update
void Start()
{
audioSource = GetComponent();
rigidBody = GetComponent();
}

// Update is called once per frame
void Update()
{
    Thrust();
    Rotate();
}

void OnCollisionEnter(Collision collision)
{
    switch (collision.gameObject.tag)
    {
        case "Friendly":
            //do nothing
            break;
        case "Fuel":
            print("Fuel");
            break;
        default:
            print("death");
            break;
    }    
}

private void Thrust()
{
    if (Input.GetKey(KeyCode.Space)) //can thrust while rotating
    {
        if (!audioSource.isPlaying)
        {
            audioSource.Play();
        }
        rigidBody.AddRelativeForce(Vector3.up * mainThrust);
    }
    else
    {
        audioSource.Stop();
    }
}

private void Rotate()
{

    float rotationThisFrame = rcsThrust * Time.deltaTime;
    rigidBody.freezeRotation = true; // take manual control of rotation
    if (Input.GetKey(KeyCode.A))
    {
        transform.Rotate(Vector3.forward * rotationThisFrame);
    }
    else if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(-Vector3.forward * rotationThisFrame);
    }
    rigidBody.freezeRotation = false; //resume physics control of rotation

}

}

Privacy & Terms