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 Exalia · Nov 04, 2013 at 02:17 PM · movementphysicstransformforceconvert

Using Add Force for character control

Hi there I've been working on a game for a while now and recently realised how adding a force to my player character could be better than changing its transform.position for collisions however I am struggling to convert my old script to use AddForce.

Here is my script (you can see a commented line of how I tried to get the same effect but failed)

         //On Screen Controls
         direction = new Vector3(joyScript.position.x, 0, joyScript.position.y);
         if (direction.magnitude > 0.1f)
         {
             Vector3 rightMovement = right * ps3speed * Time.deltaTime * joyScript.position.x;
             Vector3 upMovement = forward * ps3speed * Time.deltaTime * joyScript.position.y;
 
             Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
             transform.forward = heading;
             transform.position += rightMovement;
             transform.position += upMovement;
             //playerModel.rigidbody.AddForce(transform.forward * direction.magnitude);
             playerModel.animation.Play("runforward");
         }
         else
         {
             playerModel.animation.CrossFade("idle");
         }

If anyone could provide some assistance it would be much appreciated :) Thanks

Comment
Add comment · Show 4
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 Tanshaydar · Nov 04, 2013 at 03:42 PM 1
Share

Are you using RigidBody for your character controller? If not, it won't work.

avatar image Exalia · Nov 04, 2013 at 06:03 PM 0
Share

I've changed the code to effect the correct rigidbody

         //On Screen Controls
         direction = new Vector3(joyScript.position.x, 0, joyScript.position.y);
         if (direction.magnitude > 0.1f)
         {
             Vector3 right$$anonymous$$ovement = right * ps3speed * Time.deltaTime * joyScript.position.x;
             Vector3 up$$anonymous$$ovement = forward * ps3speed * Time.deltaTime * joyScript.position.y;
 
             Vector3 heading = Vector3.Normalize(right$$anonymous$$ovement + up$$anonymous$$ovement);
             transform.forward = heading;
             transform.position += right$$anonymous$$ovement;
             transform.position += up$$anonymous$$ovement;
             //rigidbody.AddForce(right$$anonymous$$ovement * direction.magnitude);
             //rigidbody.AddForce(up$$anonymous$$ovement * direction.magnitude);
             //rigidbody.AddForce(transform.forward * direction.magnitude);
             player$$anonymous$$odel.animation.Play("runforward");
             player$$anonymous$$odel.animation["runforward"].speed = direction.magnitude;
         }
         else
         {
             player$$anonymous$$odel.animation.CrossFade("idle");
         }

Still doesn't work however :(

avatar image Tanshaydar · Nov 04, 2013 at 06:05 PM 1
Share

You might wanna check this one out: http://wiki.unity3d.com/index.php?title=RigidbodyFPSWalker

avatar image Exalia · Nov 05, 2013 at 12:48 PM 0
Share

Thanks Tasha I was indeed trying to apply force to the incorrect rigidbody at first :)

1 Reply

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

Answer by vividhelix · Nov 05, 2013 at 06:01 AM

I would expose the magnitude, or add another factor in that multiplication there and fiddle with it in the editor. Small enough values for AddForce will not make any difference.

So make this:

 public float forceFactor;

and

 playerModel.rigidbody.AddForce(forceFactor * transform.forward * direction.magnitude);
Comment
Add comment · Show 2 · 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 Exalia · Nov 05, 2013 at 09:17 AM 0
Share

Cool I'm not at work yet but when I do I'll give it a try, thanks :)

avatar image Exalia · Nov 05, 2013 at 12:49 PM 0
Share

Thanks vivid it was that the value was too low, I was trying to change it via code, then realised I was setting it really low via the editor as it was a public variable. Thanks!

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

17 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

Related Questions

Parenting not working with massive force to rigidbody. Does having rigidbody on child screw things up? 1 Answer

How to apply in opposite direction to transform? 1 Answer

Is Physics.SyncTransforms() automatically called in built game? 0 Answers

Unity 2D Scripted Movement 2 Answers

stop vehicle from moving through walls 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