Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
4
Question by dhendrix · Feb 02, 2010 at 01:54 AM · physics

Shooting a projectile forward in 3d space

If I have a cube facing a random direction, how can I make it shoot a projectile in it's "forward" direction?

Comment
Add comment · Show 2
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 Bampf · Feb 02, 2010 at 02:25 AM 0
Share

Do you want to fire it like a projectile using Unity physics, so that it travels up in an arc, falls, maybe bounces? Or do you want the projectile to move in a straight line, more like an arcade game might do it?

avatar image dhendrix · Feb 02, 2010 at 02:56 AM 0
Share

I'm going for more of an arcade feel actually.

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Jaap Kreijkamp · Feb 02, 2010 at 02:21 AM

Basically, just instantiate the bullet with the same rotation as the cube:

var spawnDistance : float = 1.0f; // don't want the bullet spawn in centre

 // ...   
 GameObject.Instatiate(prefab, transform.position + spawnDistance * transform.forward, transform.rotation);
 // ...

and let the bullet move itself in the transform.forward direction:

var speed : float = 2.0;

function Update() { transform.position += Time.deltaTime speed transform.forward; }

or if you're using a rigidbody as bullet:

var force : float = 20.0f;

function Start() { rigidbody.AddForce(transform.forward * force); }

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
avatar image
3
Best Answer

Answer by Eduardo Meade · Feb 02, 2010 at 02:59 AM

you can use transformdirection, which ensures that the projectile will have a correct direction in wordl coordinates ( in case the object that instantiates your projectile is not a "root" object )

some code:

var newProjectile = Instantiate( projectile, transform.position, transform.rotation );
newProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed) );

we are using Vector3( 0, 0, speed ) because in Unity the default forward axis is Z

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
avatar image
0

Answer by Qamar · Dec 01, 2010 at 07:57 AM

For the Projectile you can use following:

var projectile: GameObject; var force: float;

function Update(){ if(Input.GetMouseButtonDown(0)){ projectile.rigidbody.AddForce(projectile.transform.forward projectile.rigidbody.mass force, ForceMode.Impulse);

 }

}

this will Add an immidiate force to the object to forward.

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

When dragging a Door open using RIGIDBODYSCRIPT Plays sound. 1 Answer

Determine Area using code 1 Answer

How to keep physics consistent? 1 Answer

constant force 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