Question by
CorvusMechanca · Feb 02, 2017 at 11:11 PM ·
c#rotationinstantiatepositioning
Instantiating a GameObject relative to another object's position and rotation
Heres a vid of whats happening: https://youtu.be/W8iDffafN6M
The Green bullets spawn with the correct off set relative to the object at the starting rotation but not the rotation after turning the object.
This is the code that spawns the bullet
public void bullet(){
Debug.Log (this.gameObject.transform.localPosition.z+2);
Vector3 spawnPoint = new Vector3(this.gameObject.transform.localPosition.x + 1.1f, this.gameObject.transform.localPosition.y,this.gameObject.transform.localPosition.z+1.1f);
GameObject fire = Instantiate(projectile, spawnPoint,Quaternion.identity);
fire.GetComponent<Rigidbody>().AddForce(this.gameObject.transform.up * firespeed);
}
Comment
Answer by Kishotta · Feb 03, 2017 at 06:00 AM
You can actually save yourself a lot of typing here.
public void SpawnBullet () {
// multiplying a rotation by a vector applies that rotation to the vector
Vector3 spawnPoint = transform.position + (transform.rotation * new Vector3 (1.1f, 0, 1.1f));
// ...
}