- Home /
How do I move my gameObject after the collision is detected, I just want it off screen where player cant see it.
using UnityEngine;
using System.Collections;
public class KeyScript : MonoBehaviour {
public GameObject steeldoor;
public AudioClip keySound;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.layer == 9) {
audio.PlayOneShot(keySound);
Destroy(steeldoor);
//code here for moving the object
}
}
}
Can You Give a ittle more detail on what you are trying to do exactly?
Well, Im not the programmer for this game but what I need to do is play the keySound audio clip before this.gameObject is destroyed. I could not figure it out though so I thought that ins$$anonymous$$d of destroying the object I'd just move it to where the player can't see it, but I'm also having trouble with that as well as you can see, but this is the original code my programmer gave me.
PS it is a 2D game.
using UnityEngine;
using System.Collections;
public class $$anonymous$$eyScript : $$anonymous$$onoBehaviour {
public GameObject steeldoor;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.layer == 9) {
Destroy(steeldoor);
Destroy(this.gameObject);
}
}
}
Answer by aldonaletto · Nov 24, 2013 at 01:08 AM
Use PlayClipAtPoint instead of PlayOneShot:
void OnTriggerEnter(Collider other) {
if (other.gameObject.layer == 9) {
AudioSource.PlayClipAtPoint(keySound, steeldoor.transform.position);
Destroy(steeldoor);
}
}
This function actually creates a temporary empty with an AudioSource at the desired position, and destroy it when the clip ends.
Thanks You, It worked perfectly, dont why I didn't find that solution earlier