- Home /
Instantiated Prefabs Spawning at Odd Angles
I am trying to create a weapon in a project I'm working on, and it's mostly successful except for one issue that's been plaguing me for days now. The prefabs that are instantiated by my weapon script are spawning side ways, not as in they are being fired side ways, but the orientation of the prefab itself is wrong.
Here's the weapon code:
// defines the projectile prefab
public GameObject Projectile;
// defines the point at which the prefab is instantiated
public Transform MuzzlePoint;
// creates a short delay between shots
public float timeBetweenShots = 0.3333f;
private float timestamp;
void Update ()
{
if (Time.time >= timestamp &&(Input.GetButtonDown ("Fire1")) )
{
Instantiate(Projectile, MuzzlePoint.position, MuzzlePoint.rotation);
timestamp = Time.time + timeBetweenShots;
}
}
Here's the code for the projectile:
public float thrust;
public GameObject ExplosionPrefab;
public GameObject FirePrefab;
public GameObject BloodExplosion;
void Start ()
{
GetComponent<Rigidbody>().velocity = gameObject.transform.forward * thrust;
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.GetComponent<DmgHandler>() != null)
{
Instantiate(BloodExplosion, gameObject.transform.position, gameObject.transform.rotation);
collision.gameObject.GetComponent<DmgHandler>().ApplyDamage(37);
}
else
{
Instantiate(ExplosionPrefab, gameObject.transform.position, gameObject.transform.rotation);
Instantiate(FirePrefab, gameObject.transform.position, gameObject.transform.rotation);
}
Destroy(gameObject);
}
Any help would be greatly appreciated!!
Answer by TheCheatsrichter · Jun 01, 2016 at 01:35 PM
I guess your muzzle point isnt a child of your player/weapon and therefore doesn't head in the same direction as your player is facing.
void Update ()
{
if (Time.time >= timestamp &&(Input.GetButtonDown ("Fire1")) )
{
Instantiate(Projectile, MuzzlePoint.position, transform.rotation);
timestamp = Time.time + timeBetweenShots;
}
}
It was previously a child of the weapon with the same result, I then parented it under the camera ins$$anonymous$$d however to no avail.
Your answer
Follow this Question
Related Questions
How to associate underlying data structure to prefab at runtime? 1 Answer
Instantiating an object as a child of another object 1 Answer
Having multiple objects fire prefabs in different times C# 0 Answers
Object after instantiate doesn't apply AddForce 1 Answer
All instances of a an object are being updated at once instead of the object in question. 1 Answer