• 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
1
Question by Cubber · Nov 27, 2013 at 02:40 AM · rigidbody.addforce

transform.forward problem

I am working on a tower defense game involving a tower shooting an enemy, but when the tower shoots, it shoots the bullet forward in the world direction instead of on its rotation. The bullet doesn't go forward towards the enemy but goes forward in world space off screen.

I am using the following code to shoot the bullet from the tower script,

 var ball = Instantiate(bullet, spawnPoint.transform.position, transform.localRotation);
 ball.rigidbody.AddForce(ball.transform.localPosition.forward * 5000);

bullet is a GameoObject spawnPoint is a Transform

Is there a new way to shoot something on its local forward axis? Was the .forward script changed recently, because most of the gun scripts are using it but it doesn't work for me now. How do I fix the problem and shoot it forward.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by aldonaletto · Nov 27, 2013 at 02:53 AM

transform.localPosition.forward ? This makes no sense, or will result in Vector3.forward. You should simply use transform.forward instead:

 ball.rigidbody.AddForce(ball.transform.forward * 5000);
Comment
Add comment · Show 3 · 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 Cubber · Nov 27, 2013 at 03:14 AM 1
Share

already tried transform.forward, used localposition hoping it would solve the problem. is there a way to send the rotation of the tower to the ball? I think the ball isn't taking in the rotation from the instantiate line. Sorry to change the problem type.

avatar image aldonaletto · Nov 27, 2013 at 08:02 PM 1
Share

There's another error: you should pass spawnPoint.transform.rotation to Instantiate, not transform.localRotation - change the code like this:

 var ball = Instantiate(bullet, spawnPoint.transform.position, spawnPoint.transform.rotation);

avatar image Cubber · Nov 27, 2013 at 08:05 PM 0
Share

thanks, I fixed the error and have everything working.

avatar image
0

Answer by rutter · Nov 27, 2013 at 02:58 AM

This line:

 ball.rigidbody.AddForce(ball.transform.localPosition.forward * 5000);

ball.transform.localPosition represents the ball's position, not the direction it's facing, so I'm not sure what "forward" is relative to that. To be honest, I'm surprised that compiles.

You probably mean ball.transform.forward, which uses the ball's rotation.

Comment
Add comment · Show 3 · 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 Cubber · Nov 27, 2013 at 03:16 AM 0
Share

tried transform.forward already, hoped the localposition would send it in the right direction, is there a way to send the rotation to the ball from the tower?

avatar image rutter · Nov 27, 2013 at 03:17 AM 1
Share

It looks like this script is attached to the tower? If so, transform.forward is the tower's forward direction. If not, you'll need a reference to the tower, and from there you can get its transform.

avatar image Cubber · Nov 27, 2013 at 03:31 AM 0
Share

GOT IT, changed transform.position to spawnPoint.transform.position, because it was already facing in the correct direction. Thank you for the help.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Physics in 2D mode not working? 0 Answers

ForceMode.VelocityChange vs. simply adding to .velocity 0 Answers

Basic Jump Pad help 2 Answers

rb.addforce being really buggy 2 Answers

Gameobject going past target when using rigidbody2d.addforce 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