Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by leonalchemist · May 16, 2011 at 03:48 PM · 2drotationinstantiatebulletturret

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image leonalchemist · May 16, 2011 at 05:26 PM 0
Share

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

avatar image leonalchemist · May 16, 2011 at 05:32 PM 0
Share

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 :/

avatar image leonalchemist · May 17, 2011 at 09:54 AM 0
Share

umm :/ back from sleep; alright, what should the target variable be set as? im getting a ton of error, dunno what to do...

avatar image Byterunner · May 17, 2011 at 06:32 PM 0
Share

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.

avatar image leonalchemist · Jun 16, 2011 at 03:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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

Shoot an object and have it move based on rotation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges