- Home /
Instantiated prefab doesn't collide with player
I am instantiating cubes on a mouse click, in order to allow the player to create a number of platforms for puzzle solving in game. The prefab they are associated with is Unity's standard cube with a texture and rigid body attached. However, while the prefab they are based on can be jumped on by the player and pushed, etc. the player falls through the instantiated cubes. Why is this?
Here is the script I have written:
public class ProjectileGeneratorScript : MonoBehaviour {
public GameObject crystalCube;
float nextSummon=0;
public float summonRate = 0.5f;
public float cubeSpeed = 10;
public AudioClip summonFX;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetButtonDown("Fire1") && (Time.time > nextSummon))
{
//print ("Bullet fired.");
nextSummon = Time.time + summonRate;
//Instantiate instance of projectile and cast to game object.
GameObject cloneCube = (GameObject)Instantiate(crystalCube,transform.position,transform.rotation);
cloneCube.rigidbody.velocity = transform.TransformDirection(new Vector3(0,0,cubeSpeed));
Physics.IgnoreCollision(cloneCube.collider,transform.root.collider);
//play fx
//audio.PlayOneShot(summonFX);
}
}
}
What is this doing: Physics.IgnoreCollision(cloneCube.collider,transform.root.collider);
What object is this script attached to?
That ^^^ simply makes sure that the bullet doesnt collide with the thing that instantiated it, (Camera?)
@getyour411 my assumption is that it did what @DryTear indicates. I thought the script was on a sphere attached to the character. But if the root is considered the parent object, that perhaps that is negating the character's collider. I will take a look at that, it seems like that could be the issue, although, does Physics.IgnoreCollision() permanently turn off collision for its two arguments?
Your answer
Follow this Question
Related Questions
how Load Prefabs after build 0 Answers
Call a function in an instanciated prefab has no effect on the prefab 2 Answers
I cannot get my code to function correctly, and do not know what to do. Please help. 1 Answer
Rigidbodies violently repel when replacing with pre-shattered opject 2 Answers
How to randomly instantiate other prefabs parallel? 1 Answer