Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 inglipX · May 23, 2013 at 12:33 AM · rigidbodyaddforcecube

Rigidbody add force relative to any rotation

Hi, currently my main camera holds the cube in front of it, and as I look around the cube rotates as i am turning. My script now only will shoot the rigidbody straight on the right mouse click if the camera is in such a position that the cube is rotated straight, aligned with the camera (player). How can i make this work when i look at any direction and the cube is turned in any rotation as i look and then I can press right mouse to shoot it forward in that direction? In my fixed update i currently am using transform.forward, which seems to only shoot it correctly when the rotation is near 0. Thanks!

 var currentObject : GameObject;
 var spawnTo : GameObject;
 var objectHeld : boolean = false;
 var force : float = 40.0f;
 
 @HideInInspector
 var lerpPosition : float = 0.0f;
 @HideInInspector
 var lerpTime : float = 2.0f;
 
 
 function Update (){
 
 
 
 
     if (Input.GetMouseButtonDown(0)){
     var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, hit, 8)){
             if (hit.rigidbody){
                 currentObject = hit.rigidbody.gameObject;
                 objectHeld = true;
                 }    
         }
 }
 
     
     
     if (!Input.GetMouseButton(0)){
         objectHeld = false;
         lerpPosition = 0.0;
     
     }
 
     if (objectHeld){
         lerpPosition += Time.deltaTime/lerpTime;
         currentObject.transform.position = Vector3.Lerp(currentObject.transform.position, spawnTo.transform.position, lerpPosition);
         currentObject.rigidbody.useGravity = false;
         currentObject.rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionZ;
         
         
     }
 
     if (!objectHeld){
         currentObject.rigidbody.useGravity = true;
         currentObject.rigidbody.constraints = RigidbodyConstraints.None;
 
     }
     
     
     
  
     
     
 
 }
 
 
 function FixedUpdate ( ) {
 
 
     if (Input.GetMouseButtonDown(1) && objectHeld){
         objectHeld = false;
         
         currentObject.rigidbody.AddRelativeForce(transform.forward * force);
         }
 
     }
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
2
Best Answer

Answer by sparkzbarca · May 23, 2013 at 01:04 AM

addrelativeforce adds forces that shoots in it's local space.

basically

transform.forward == that objects version of forward

but you don't want that to shoot straight out.

you dont give a damn what the object thinks is forward. forward is what the camera thinks is forward

if this script is attached to the camera simply use addforce instead of addrelativeforce.

if this is not

currentObject.rigidbody.AddForce(camera.main.transform.forward * force)

mark as answered if your happy witht he answer

Comment
Add comment · Show 8 · 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 inglipX · May 23, 2013 at 09:59 AM 0
Share

Hi, thank you but i have already tried using Camera.main.transform.forward and it still doesnt fix the issue. :(

avatar image sparkzbarca · May 23, 2013 at 10:58 PM 0
Share

did you use relative force or addforce?

avatar image sparkzbarca · May 23, 2013 at 11:02 PM 0
Share

oh wait your doing a couple wierd things here

first off you can't use input in fixedupdate.

Secondly you shouldn't do the if(!objectheld) in update you should just do it when you release the update because it won't run between the release and the addforce

avatar image inglipX · May 23, 2013 at 11:11 PM 0
Share

i don't understand what you mean

avatar image sparkzbarca · May 23, 2013 at 11:24 PM 0
Share

input only works in the frame your in. the update your in.

fixed update only happens every so many updates.

to use fixed update you need to do.

 void update()
 {
 if (intput.getmousebutton(0))
 {
 mousedown = true;
 }
 
 }
 
 void fixedupdate()
 {
 if(mousedown)
 {
 //do something;
 }
 }

because mouse down can happen between fixedupdates and be missed

do not use addrelativeforce use addforce relative is in the objects space. add force is in global space.

since you don't want to be in the objects space you want to be in the camera space you just move relative to world space and give the camera's world space to addforce

also because you freeze position in z in you need to release BEFORE YOU ADD FORC$$anonymous$$

you can't add force in that direction and get movement if the constrint is there

if your code you have

 if (mouse down)
 
 object held = false;
 
 add force;

release constraint, because that update runs seperatly your not in that scope it'll run after this

what you need is

 if(mouse down)
 {
 objectheld = false
 release constraint 
 addfoce
 }
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

14 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

Related Questions

How do I make a cube fly along a single axis. 1 Answer

rb.AddForce problem 2 Answers

How to have a cube to fall on fixed rotation? 1 Answer

How to move a cube constantly with GetKeyDown 3 Answers

Making an object move in the direction it's faceing. 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