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 Carl 2 · Mar 25, 2011 at 11:32 AM · rigidbodyforceballaddtorque

Adding force in relation to the camera issues

Hi everyone. I'm having a bit of trouble with my script for adding force to the player controlled object in a project. I've got the force applying to the object (it's a ball with a rigidbody) when pressing the W key and it works fine for whichever direction the camera is facing, but I can't seem to figure out how to get it to apply force for the A, S and D keys in the proper directions.

Here is the script:

function Update () {

if (Input.GetKey ("w")) rigidbody.AddTorque(Camera.main.transform.right * 3000);

 if (Input.GetKey ("d"))
     rigidbody.AddTorque(Camera.main.transform.right * 2000);

 if (Input.GetKey ("s"))
     rigidbody.AddTorque(Camera.main.transform.up * 2000);

 if (Input.GetKey ("a"))
     rigidbody.AddTorque(Camera.main.transform.right * 2000);

}

If anyone could shed any light on the situation for me, that'd be great :)

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
1

Answer by AngryOldMan · Mar 25, 2011 at 11:42 AM

1st of all map your input keys so they are all named with relative conventions. for example use names such as W,a,s,d, instead of w,right,down,left. This would be your 1st port of call for checking what's wrong, go to edit ~> project settings ~> input to check the correct naming conventions and keys of your valid inputs.

Second of all using forces as high as you are (2000 +) means that your level and objects may be scaled too big, i suggest keeping your player unscaled then scaling everything elese around your player, I promise you, you will only benifit from this (but don't forget to reduce all of your forces to suit)

Thirdly are you having an errors, if so what do they say and what line are they on? if no errors then put debug logs in all over your script so you can tell exactly how the script is executing itself.

Finally the only problem I could tell of is the direction that your adding torque, I'm assuming you want to rotate the object/camera right? otherwise use rigidbody.AddForce to move it around.

if (Input.GetKey ("w")) rigidbody.AddTorque(Camera.main.transform.forward * 3000);//or transform.up

if (Input.GetKey ("right")) rigidbody.AddTorque(Camera.main.transform.right * 2000);

if (Input.GetKey ("down")) rigidbody.AddTorque(Camera.main.transform.forward * -2000);//or transform.up

if (Input.GetKey ("left")) rigidbody.AddTorque(Camera.main.transform.right * -2000);

Comment
Add comment · Show 4 · 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 Carl 2 · Mar 25, 2011 at 02:56 PM 0
Share

Thanks for the response. No, there are no errors when I run it, but the weird thing is, if i add force to forward on the W key, the ball will roll to the left and when I tell it to add force to the right e.g Camera.main.transformright) the ball will roll forwards whichever way the camera is facing.

A work around for it could be that I can put in add force to the right to go forwards and adding force forward to make the ball roll left, but then I don't know how to make it roll backwards and right.

Thanks for your time :)

avatar image AngryOldMan · Mar 26, 2011 at 02:09 AM 0
Share

that is strange, check your transform.localPosition for the object and make sure it's not parented to anything, it sounds like your object is parented to your camera if so unparent it and ins$$anonymous$$d parent your camera to your game object! the answer above explains how to move them in the other directions, just add a negative number ins$$anonymous$$d of a positive, viola, so if +1 is move forward then -1 is move back, same as if +1 is move right -1 is move left. Post back with the problems if this doesn't help :)

avatar image Carl 2 · Mar 26, 2011 at 09:55 AM 0
Share

I couldn't figure out why right moved forward and forward move left etc (the ball or camera wern't parented to anything either and when I tried to parent the camera to the game object it sent the ball flying off the level for unknown reasons :S), but on a good note I now have the ball moving EXACTLY how I wanted it to by just using the positive and negitive numbers like you suggested.

Thankyou very much for taking the time to help me with the problem, you've helped me out tremendously.

Thanks :)

avatar image AngryOldMan · Mar 26, 2011 at 11:46 AM 0
Share

ah... I think the problem is the massively high numbers for force your appling, try parenting the game object to the camera and reducing your force number, or better still make it a variable so you can control it in the inspector rather than messing about with scripts everytime. been a pleasure to be of use

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

Hooking RigidBody.AddForce 0 Answers

Calculate force for torque by specifying a required velocity 1 Answer

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

Disabling Kinematic on hit 2 Answers

How can i make a ball jump in a curve from one place to another and vice versa. Like juggling a ball ! 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