- Home /
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
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);
}
}
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
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
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
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?
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
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. :)
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;
yea but I want a rigidbody ... so how do I do it?
Your answer
Follow this Question
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