Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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
0
Question by PleaseGiveMeFreeThingsTy · Aug 13, 2016 at 03:28 PM · scripting problemmovementrigidbodyaddforcemovement script

Why can't i addforce to a rigidbody?

private Rigidbody rb; rb = GetComponent(); rb.AddForce ()

it doesn't work i will show you the entire code here

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 PleaseGiveMeFreeThingsTy · Aug 13, 2016 at 02:45 PM 0
Share

using UnityEngine; using System.Collections;

public class player : $$anonymous$$onoBehaviour {

 public float movespeed;
 private Rigidbody rb;

 // Use this for initialization
 void Start () 
 {
     rb = GetComponent<Rigidbody> ();
 }
 
 // Update is called once per frame
 void Update () 
 {
     float inputX = Input.GetAxis ("Horizontal");
     float inputZ = Input.GetAxis ("Vertical");

     float moveX = inputX * movespeed * Time.deltaTime;
     float moveZ = inputZ * movespeed * Time.deltaTime;
     print (moveX + moveZ);

     rb.AddForce(moveX, 0f, moveZ);
 }

}

avatar image PleaseGiveMeFreeThingsTy · Aug 13, 2016 at 02:46 PM 0
Share

i have to specify that : i changed movement speed to 200 i have a rigidbody on my player and i attached the script to the player

avatar image BloodBTF PleaseGiveMeFreeThingsTy · Aug 13, 2016 at 06:45 PM 0
Share

You might need to specify the forcemode.

 rb.AddForce(moveX,0f,moveZ,Force$$anonymous$$ode.Accelaration);
 //idk if i spelled Accelaration right but you get the idea


avatar image PleaseGiveMeFreeThingsTy BloodBTF · Aug 14, 2016 at 12:26 PM 0
Share

using UnityEngine; using System.Collections;

public class player : $$anonymous$$onoBehaviour {

 public float movespeed;
 private Rigidbody rb;

 // Use this for initialization
 void Start () 
 {
     rb = GetComponent<Rigidbody> ();
 }
 
 // Update is called once per frame
 void Update () 
 {
     float inputX = Input.GetAxis ("Horizontal");
     float inputZ = Input.GetAxis ("Vertical");

     float moveX = inputX * movespeed * Time.deltaTime;
     float moveZ = inputZ * movespeed * Time.deltaTime;
     print (moveX + moveZ);

     rb.AddForce(moveX, 0f, moveZ,Force$$anonymous$$ode.Acceleration);
 }

}

I did this still doesn't work

2 Replies

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

Answer by Drakonno · Aug 14, 2016 at 02:21 PM

Your first code:

 using UnityEngine;
 using System.Collections;
 
 public class player : MonoBehaviour {
 
     public float movespeed;
     private Rigidbody rb;
 
     // Use this for initialization
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         float inputX = Input.GetAxis("Horizontal");
         float inputZ = Input.GetAxis("Vertical");
 
         float moveX = inputX * movespeed *Time.deltaTime;
         float moveZ = inputZ * movespeed *Time.deltaTime;
         print(moveX + moveZ);
 
         rb.AddForce(moveX, 0f, moveZ);
     }
 }


@PleaseGiveMeFreeThingsTy is flawless. I've already checked.

Create capsule. Add rigidbody. Open constraints, freeze rotations in all axes, for tests probably also position in Y-axis. Set collision detection to continuous dynamic. Add Your script. Set move speed to 200. Make sure You have in: Edit -> Project Settings -> Input, declared axis "Horizontal" and "Vertical".

You must have missed something in Your setting it up. Check also what is displayed in console, maybe something other modify Your speed and is always printed 0?

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 PleaseGiveMeFreeThingsTy · Aug 17, 2016 at 01:46 PM 0
Share

look I did everything that you tell me I freeze the rotation I position the capsule collider in Y-axis set the collision to continuous dynamic from the rigidbody(it was on discrete) I set the move speed 200(when I click play is always 200 ) I added the script the chech at edit-project-settings-input-I have the horizontal and vertical axes horizontal(A,D) vertical(S,W)

And it didn't work but,i see it was moving the position just a little bit so I made the movespeed 400 and it works .... TY

avatar image Drakonno PleaseGiveMeFreeThingsTy · Aug 17, 2016 at 02:55 PM 0
Share

I'm glad that worked. :)

That "continuous dynamic" was on testing purpose. If Your object moves really fast, he might not get all collisions on his way, and just go through the others objects. $$anonymous$$ost of the time 'discrete' is enough.

I can't find however if the "time.deltatime" is needed. There was one way to move objects, that doesn't need that multiplication. If You have multiple machines You can check if the movement is the same without "time.deltatime". After all, removing it from Your code also will do the job, and make an object move... more. :)

avatar image
0

Answer by afonsolfm · Aug 13, 2016 at 11:05 PM

That's not how RigidBody.AddForce works.

It seems you're intention is of moving with a constant speed, you don't really need a rigidbody.

Try

     Vector3 addPos = new Vector3();
     addPos.x = inputX * movespeed * Time.deltaTime;
     addPos.y = 0;
     addPos.z = inputZ * movespeed * Time.deltaTime;
 
     transform.position += addPos;

Comment
Add comment · Show 1 · 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 PleaseGiveMeFreeThingsTy · Aug 14, 2016 at 12:22 PM 0
Share

yea but I want a rigidbody ... so how do I do it?

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to move Character with AddForce 1 Answer

How do i maintain the same speed in the air? 1 Answer

moving with rigidbody without acceleration 0 Answers

Trouble in freezing movement in z axis 1 Answer

Player isn't moving (AddForce) (Hook)(c#) 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