I’m following a tutorial (How to Make an Object Shatter Into Smaller Fragments in Unity) but when I click the space bar, the prefab gets instantiated where I originally created it, not where I’ve transformed it. Only difference in my scene is that I wrote in C# not JavaScript.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestructionCtrler : MonoBehaviour {
public GameObject remains; // this is the prefab, assigned manually in the editor
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.Space))
{
Instantiate (remains, gameObject.transform.position, gameObject.transform.rotation);
Destroy (gameObject);
}
}
}
At runtime, the prefab also instantiates as a clone (not sure if that’s relevant).
Hello, i did not watch tutorial but I am pretty sure that your code if fine.
i guess your problem causes because your prefabs (or game object) have wrong transforms.
i suggest you to click your game object and reset its transform first.
then do this for its every child too (then you may need to re-place them by hand)
after this, you need to reset all your prefabs too.
I sıggest you to check this tutorial please pay attention to parent-child transforms.
As my exp, You should compare your practice and the provided source by the tutorial. You may find out where the problem is. And In my opinion, your code is no problem. I suggest you try to split the instantiate and set position process just like this:
GameObject go = Instantiate (remains) as GameObject;
go.transform.position = transform.position;
go.transform.rotation = transform.rotation;
or you can try this:
Instantiate (remains, transform.position, transform.rotation);
Maybe it’s work.
Cheers!!!
What I realized was that an Animation in my Prefab was causing its position to get altered every time. Changing the animation to remove position properties fixed this issue.