Object spawn rotation
Hi so i have a spawn script for a bullet. And. When it spawns, it doesnt spawn with the same rotation as the object the it spawns from.
Could some tell me what to do?
Heres my code
using UnityEngine; using System.Collections;
public class SpawnProjectile : MonoBehaviour {
public GameObject projectile;
public float ROF = 0.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0) & ROF >= 10.0f) {
Instantiate (projectile, transform.position, Quaternion.identity);
ROF = 0.0f;
}
if (ROF < 10.0f) {
ROF += 0.1f;
}
}
}
Answer by rob5300 · Jan 14, 2016 at 10:26 AM
That is because Quaternion.identity
is a pre-made Quaternion witn no rotation (0,0,0). I would assume you can see when you execute that the projectile has this rotation.
You already did the right thing by using transform.position
when Instantiating projectile
.
You want to pass transform.rotation
from the parent object you mention. Assuming this MonoBehaviour
is attached to it you can get that my referencing ether transform.rotation
, this.transform.rotation
or GetComponent<Transform>().rotation
.
Excuse me if there are errors in this, i haven't used Unity for a little while.
You would replace Instantiate (projectile, transform.position, Quaternion.identity);
with:
Instantiate (projectile, transform.position, transform.rotation);
Note that we have altered the 3rd argument from referencing Quaternion.identity, a blank rotation value of 0,0,0 to using transform.rotation, which is the rotation data from the Gameobject this script is attached to.
Answer by DarKMaskMaster · Apr 18, 2020 at 12:31 PM
Hello rob5300
I am trying to get that running on a timer for an enemy but it doesnt sem to work. can you help me out? This is the script:
public GameObject projectile; public float ROF = 0.0f; // Use this for initialization void Start() {
}
public float targetTime = 0.1f;
void Update()
{
targetTime -= Time.deltaTime;
if (targetTime <= 0.0f)
{
timerEnded();
}
}
void timerEnded()
{
Instantiate(projectile, transform.position, transform.rotation);
ROF = 0.0f;
if (ROF < 10.0f)
{
ROF += 0.1f;
//do your stuff here.
}
// Update is called once per frame
}