- Home /
Weird translation after instantiating on transform.rotation
Hi, I'm making a basic shooting script, but whenever I rotate my player around, my bullets seem to be rotated in all the wrong directions. I'm basically instantiating them with my players transform position and rotation, and then have them move with transform.forward using a script on the bullet. I don't get why the rotations are acting like this tho.
Here's my 2 scripts, simple yet not working properly:
#pragma strict
var bullet: GameObject;
var offset: Vector3;
var fireRate: float = 1;
var curRot: Quaternion;
var curPos: Vector3;
function Awake()
{
InvokeRepeating("Shoot",0, fireRate/10);
}
function Update()
{
offset = transform.forward * 10;
curRot = transform.rotation;
curPos = transform.position + offset;
}
function Shoot()
{
if(Input.GetMouseButton(0))
{
Instantiate(bullet,curPos,curRot);
}
}
and for the bullet:
#pragma strict
var bulletForce: float = 2;
var player: GameObject;
function Awake()
{
player = GameObject.FindWithTag("Player");
Destroy(gameObject, 3);
}
function Update()
{
transform.Translate(transform.forward * bulletForce);
}
Anyway, to me this looks like it should work, yet it seems somewhat like I'm multiplying the rotation somehow, as the further I rotate from 0, the farther off the rotations on the bullets seem to be.
Any Idea's?
PS: I do have a rigidbody with a drag of 10 and an angular drag of 1 attached to my player, could this have something to do with it?
Answer by whydoidoit · Jun 30, 2012 at 10:22 PM
Translate is already working in local space, so using transform.forward is doubling the effect. Use Vector3(0,0,1) instead or set the second parameter of Translate to Space.world.
Ah, of course, makes sense! Guess I still have a lot to learn even about the basics of unity...xD
Your answer
Follow this Question
Related Questions
How do drag and angular drag interact? 2 Answers
drag object including free rotation 0 Answers
Move object forward in relation to the player 1 Answer
How to have an object inherit rotation of player 1 Answer
Drag area from HUD 1 Answer