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
1
Question by HolBol · Apr 16, 2010 at 04:43 PM · camerabuttonmousedirectionforce

How can I apply force in the direction of the camera when the user presses the mouse

Is there a way to, when the person presses the mouse button, apply force in the direction the camera/ object is facing? I have the adding force part, i just need it for the direction the object is facing.

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
4

Answer by duck · Apr 16, 2010 at 04:51 PM

Yes, all GameObjects (including cameras) have a transform component, and on the transform class is a .forward variable which indicates the forward direction of that object as a vector.

So, for the main camera's direction, just use this as the force direction vector:

Camera.main.transform.forward

Or for any other object:

thatObject.transform.forward

Or for the forward direction of the object that the script is on, simply:

transform.forward

Multiply it by whatever you want to control the strength.

As an example, you could place this script onto any object that you would like to be "pushed" when clicked on:

var forceStrength = 50.0; private var applyForce = false;

function OnMouseOver() { if (Input.GetMouseButton(0)) { applyForce = true; } else { applyForce = false; } }

function OnMouseExit() { applyForce = false; }

function FixedUpdate() { if (applyForce) { rigidbody.AddForce(Camera.main.transform.forward * forceStrength); } }

Any gameobject with this script attached (and which has a rigidbody) will be "pushable" with the mouse, and will be pushed in the direction of the camera's forward vector.

Comment
Add comment · Show 6 · 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 HolBol · Apr 16, 2010 at 04:55 PM 0
Share

would you put that like this?

if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0)) {
transform.forward; } }

??

avatar image HolBol · Apr 16, 2010 at 04:58 PM 0
Share

that didn't work. I need it so it always travels the direction it's facing, but not fixed on the Axis.

avatar image duck ♦♦ · Apr 16, 2010 at 05:19 PM 0
Share

added example. If this isn't close to what you're looking for, please explain in more detail.

avatar image HolBol · Apr 16, 2010 at 09:20 PM 0
Share

Ah thanks, found a simpler way by adding Constant relative force to the object ins$$anonymous$$d, but i may need this for something else :D

avatar image venhip · Jul 15, 2012 at 12:22 AM 0
Share

Ben, what would it be if the script was attached to the player character and not the object you click on? (i did ask the question on here, if you need more info you could find it) Thanks.

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

Adjust camera rotation while looking at a target 1 Answer

Camera Rotation 1 Answer

Move character in direction its facing 1 Answer

Camera Control Help 0 Answers

Camera rotation - button emulation 0 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