- Home /
Instantiation causing issues x,y
I've been fighting this a few days now. I've been playing with a few basic primitives to get a top down view working, as well some some basic form of shooting. However when I rotate the object the instantiated bullets fires off in two directions seemingly on the opposite axis from what that character primitive is facing. Here's what I have atm in the script which isn't much. I'm using the x,y axis and have the Z poking me in the face. The instantiation also seems to stay in the middle of the camera view instead of spawning where the primitive has been moved to. Any thoughts or at least what I'm doing wrong would be appreciated.
function Update () {
if(Input.GetKey(KeyCode.Space))
fire();
if (Input.GetKey(KeyCode.RightArrow))
//turret.transform.rotation.z += 0.01 * moveSpeed * 100;
turret.Rotate(Vector3(turret.position.x,turret.position.y,100) * Time.deltaTime);
print(turret.rotation);
if (Input.GetKey(KeyCode.LeftArrow))
//turret.transform.rotation.z -= 0.01 * moveSpeed * 100;
turret.Rotate(Vector3(0,0,-100) * Time.deltaTime);
print(turret.rotation);
}
function fire() {
var clone = Instantiate(bullet, turret.transform.right , turret.transform.rotation);
bullet.rigidbody.AddForce(transform.forward * speed);
}
Answer by DaveA · Mar 14, 2012 at 12:23 AM
Instantiate(bullet, turret.transform.right
turret.transform.right is a direction, defaulting to 1,0,0, so to use it as a position, not too good.
If you want the bullet to spawn at the end of the muzzle, you can put a dummy (empty) object (transform) at the end of the muzzle (parented to the muzzle), and get its world position at the time of firing.
Then add force in the direction the turret is facing, namely, turret.transform.forward (probably)
Thanks for the answer. I fixed half the issues thanks to your input, but I'm still finding the turret end is firing from 2 ends of the primitive. It doesn't seem to matter if the addforce is in there or not. http://s16.postimage.org/3ke6tsvjp/Untitled.png is a screen of what's going on.