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 DevMerlin · Feb 03, 2014 at 05:50 AM · transform.gun movementgun position

How do I add rotation to movement so that a bullet does not travel in a straight line?

I know this is one of those questions most likely asked a dozen times, but I'm having some serious trouble with this. I'm working on a 2D sidescroller, and my character can lean back and forth to shoot a gun. However, the particle bullet only travels in a singular straight direction, ignoring all attempts to add the starting rotation to it's direction of travel. I want the particle to travel in the direction the gun is aimed towards, which is based on the spine of the character.

 Transform bullet = Instantiate(particleFire, launchLocation.position, bone.transform.localRotation) as Transform;
 
 bullet.LookAt(bone.transform.localRotation.eulerAngles);
 bullet.rigidbody.velocity = Vector3.right * 8;


Comment
Add comment · Show 1
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 robertbu · Feb 03, 2014 at 06:08 AM 0
Share

LookAt takes a position in world space, not an angle. How you solve this problem will depend on your models and what you define as the 'forward' in your 2D environment. Are they all sprites, or is part/all of them 3D models? If the 'bone' has a rotation of (0,0,0), what direction defines 'forward'?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by supernat · Feb 03, 2014 at 06:06 AM

Parent the emitter to the gun if you're using an emitter, which I can't tell from the code but doesn't look like you're using a particle system here. If you're just instantiating an object, the direction it travels will be determined by the script driving it or the physics.

LookAt takes a position, not a rotation. Eulerangles are your rotation values. Also, it appears you were attempting to tell the bullet to look at the bone. Usually you use LookAt when you want to adjust the eulerAngles of a transform such that the object is facing he target. So I don't think you want it to look at the bone.

It seems you are relying on the physics to move the bullet, so by telling the bullet to move with a velocity to the right, it will move in a straight line along the right axis (+X) as you are in world coordinates, not body. I think you can use rigidbody.AddRelativeForce(Vector3.right*8) or use Vector3.forward depending on your setup. You would need to parent the bullet to the gun (bullet.transform.parent = gun.transform) first. Then the AddRelativeForce should work. You might have to tweak it if the gun transform is not facing down the barel in body cords.

Comment
Add comment · 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

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Time.deltaTime not working? 1 Answer

How can I make an object always look at the mouse? 2 Answers

How to make a gun work with camera? 1 Answer

Bullet won't move with gun? 2 Answers

Fix gun so it moves with the character correctly when changing animations 2 Answers


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