Collision problems

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

public class Rocket : MonoBehaviour {
    [SerializeField]float rcsThrust = 100f; // makes switches for your game
    [SerializeField] float mainThrust = 100f;
    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip success;
    [SerializeField] AudioClip death;
    [SerializeField] float levelLoad = 1f;
    [SerializeField] ParticleSystem mainEngineParticles;
    [SerializeField] ParticleSystem successParticles;
    [SerializeField] ParticleSystem deathParticles;
   
    Rigidbody rb; // fucntion to var
    AudioSource audioSource;

    enum State { Alive, Dying, Transcending}
    State state = State.Alive;
    // Use this for initialization
	void Start () {
        rb = GetComponent<Rigidbody>(); // calling out the componets
        audioSource = GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (state == State.Alive)
            // todo stop sound somewhere
        {
            RespondToRotate();
            RespondToThrust();
        }
    }
    void OnCollisionEnter(Collision collision)
    {
        if (state != State.Alive) { return; }  /// ignores collision when dead
        
        switch (collision.gameObject.tag) // tells the game....

        {
            case "Finish":
                StartSuccess();
                break;

            case "Friendly":
                // if the tag says Friendly, say OK
                print("OK");
                break;

            case "Enemy":
                // if the tag says enemy, say you died
                StartDeath();
                break;



        }
    }

    private void StartDeath()
    {
        state = State.Dying; 
        audioSource.Stop();
        audioSource.PlayOneShot(death);
        deathParticles.Play();
        Invoke("LoadFirstLevel", levelLoad);
    }

    private void StartSuccess()
    {
        state = State.Transcending;
        audioSource.Stop();
        audioSource.PlayOneShot(success);
        successParticles.Play();
        Invoke("LoadNextLevel", levelLoad);
    }

    private  void LoadNextLevel()
    {
        SceneManager.LoadScene(1); // allow for more than 2 levels
    }

    private void LoadFirstLevel()
    {
        SceneManager.LoadScene(0);

    }


    void RespondToRotate()
    {
        rb.freezeRotation = true; // take manual control over rotation

        float rotationThisFrame = rcsThrust * Time.deltaTime; // time delta is FPS

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(Vector3.forward * rotationThisFrame);
        }
       
         else if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(-Vector3.forward * rotationThisFrame);
        }
        rb.freezeRotation = false; // resume physics control
    }

     void RespondToThrust()
    {
        ApplyThrust();

    }

    private void ApplyThrust()
    {
        float ThrustThisFrame = mainThrust * Time.deltaTime;
        if (Input.GetKey(KeyCode.Space))
        {
            rb.AddRelativeForce(Vector3.up * ThrustThisFrame);
            if (!audioSource.isPlaying)
            {
                audioSource.PlayOneShot(mainEngine);
            } // so it doen't layer
            mainEngineParticles.Play();
        }
        else
        {
            audioSource.Stop();
            mainEngineParticles.Stop();
        }
    }
}

The code works, the only problem is when i enter in unity and play the game. If i hit a wall, you are supposed to see explosion particles. Instead they only play for a split second and restart my level.

Thanks in advance for any help

Morning Roman,

Just looking at your Invoke for loading the next level.

Invoke("LoadNextLevel", levelLoad);

have you tried increasing the levelLoad value up to say 4f seconds just to check if thats enough time for the particles to play as its currently set to 1 second.

I tried that it still only plays for .5 seconds then starts from the beginng. I added a moving object script on one of the objects, does that have any effect?

using System.Collections.Generic;
using UnityEngine;

[DisallowMultipleComponent] //(Attribute) This is only allowing one componet for the game object
public class Occulator : MonoBehaviour
{

    [SerializeField] Vector3 movementVector= new Vector3(10f, 10f, 10f);
    [SerializeField] float period = 2f;
    [Range(0,1)][SerializeField] float movementFactor; // 0 for not moved, and 1 for fully moved.

    Vector3 startingPos;

	// Use this for initialization
	void Start () {
        startingPos = transform.position;
	}
	
	// Update is called once per frame
	void Update () {
        if (period <= Mathf.Epsilon) { return; }
        float cycles = Time.time / period;

        const float tau = Mathf.PI * 2f;
        float rawSineWave = Mathf.Sin(cycles * tau);

       

        movementFactor = rawSineWave / 2f + 0.5f;

        Vector3 offset = movementFactor * movementVector;
        transform.position = startingPos + offset;
	}
}

wouldnt have thought so as its not dealing with any of the collision code.
Are you using any source control? just wondering if you have the project online somewhere i can download it for a look… nothing i can see is immediately obvious, unless im missing something.

Just a thought, check what value you have for the levelLoad in the Inspector, the value here will override whatever you have set when you declared it, e.g. the 1f.

1 Like

Thanks the level load was set to zero. should I not serialize it so it does not do that anymore.

2 Likes

You can still serialize it, the point of doing so is so that you, with your designer hat on can tweak the values that the developer, you with your other hat on, has exposed.

The main thing is that now you know how Unity will behave you can work with it.

On a related note, you can get the duration of ParticleSystem effects and/or AudioClips from within code, so as a TODO for.later sith your project you may want to consider grabbing amd using those values in code instead of a value supplied from the Inspector, as different effects amd clips may have differing durations :slight_smile:

Hope this is of use.

1 Like

good catch @Rob :slight_smile:

1 Like

…a lucky guess my friend :slight_smile:

1 Like

Privacy & Terms