- Home /
Turret bullet rotation problem
i've been stock on this for a like 2-3 days, i have a turret that instantiate a bullet prefab at the player every second within range, i want the bullet to face the player which doesnt. btw this is a 2D sidecroller, i've tried a 100 things that doesnt work..
the turret is compose of a base and the part that shoots and point towards the player as a child object
(the bullet's rotation seems to shoot according to the base's rotation which is not i what i want, i think only the z rotation should be facing the player) here's some of my code so far:
***this script is attached to the turret base:
Instantiate(TurretBullet, transform.position, transform.rotation);
***and then ths script is attached to the bullet prefab:
var projectileSpeed : float = 10;
var bulletMove : float;
var targetObj : GameObject;
function Update () {
bulletMove = projectileSpeed * Time.deltaTime;
transform.position += transform.forward * bulletMove;
}
still needs some help plz.
Answer by Byterunner · May 16, 2011 at 04:14 PM
Your instantiate command is causing the bullet to inherit the rotation of the base by telling it to use transform.rotation. Instead you'll want to calculate the rotation needed to point the bullet at the player. Try the following where target is the player.
Vector3 relativePos = target.position - transform.position;
Quaternion newRotation = Quaternion.LookRotation(relativePos);
Instantiate(TurretBullet, transform.position, newRotation);
UCE0001: ';' expected. Insert a semicolon at the end. heyh wat? i get that for at both 1st 2 lines, code looks fine to me though?..
all i changed was the target position like Vector3 relativePos = targetObj.position - transform.position; while targetObj being a GameObject set to the player in unity
also i forgot to ask in the question, how do i instantiate slightly in front the turret gun
umm, this is javascript btw, i think i just had little sleep today :/ though how do u set the target? i dont rly have experience with the target function. cause i get: BCE0019: 'position' is not a member of 'UnityEngine.GameObject'. pretty sure its a very noob mistake though :/
umm :/ back from sleep; alright, what should the target variable be set as? im getting a ton of error, dunno what to do...
If targetObj is a GameObject, you'll need to use targetObj.transform.position to extract the position. As far as sorting out all of the errors, you'll probably do yourself a world of good by going through all of the tutorials you can before trying to make your own thing.
right its been a while, i tried to use the code above, made sense to me and didnt get an error but i didnt seem to actually do wat i wanted. i still was getting the default rotation on the bullet
Your answer
Follow this Question
Related Questions
Modify the rotation of Instatiate prefab 1 Answer
Instantiate rotation not working 1 Answer
Rotate Bullet with rotating Turret PROPERLY 2 Answers
2d turret problem 1 Answer