0
\$\begingroup\$

An object (Wall) is continuously moving forward on the X axis with a simple script:

public float speed = .2f;
void FixedUpdate()
{
    transform.Translate(speed, 0, 0);
}

It is pushing an other object (Penguin), which is affected by gravity. It can "jump" too, with a script of:

public void Jump()
{
    transform.position = new Vector3(transform.position.x, transform.position.y + 1.15f, transform.position.z);
}

When jumping, over a certain value of speed, like 0.2f, the Penguin is not falling to the ground until it reaches a velocity Vector of around (0.0, -10.0f, 0.0).

Setting the speed to 0.02f makes it instantly fall.

How can I achieve the same result with higher speed value?

I attached the setup of the two object in inspector:enter image description here

\$\endgroup\$
4
  • 2
    \$\begingroup\$ If you want these objects to interact physically, you should treat the transform component as off-limits and move them purely with rigidbody methods. \$\endgroup\$
    – DMGregory
    Jan 17 '20 at 12:41
  • 2
    \$\begingroup\$ In this case, MovePosition for the wall, and AddForce or velocity for the penguin \$\endgroup\$ Jan 17 '20 at 15:59
  • \$\begingroup\$ @LouisGarczynski could you add this as an answer so I can accept it? \$\endgroup\$ Jan 20 '20 at 11:03
  • \$\begingroup\$ @LuisGarczynski could you check this topic too please? gamedev.stackexchange.com/questions/178484/… \$\endgroup\$ Jan 20 '20 at 14:57
2
\$\begingroup\$

To move a rigidbody, there's a few different tools:


If it is not kinematic, use rigidbody.velocity or rigidbody.AddForce. For example

rigidbody.velocity = Vector3.forward * Input.GetAxis("Vertical") * playerSpeed;

If it is kinematic, use MovePosition:

rigidbody.MovePosition(rigidbody.position + Vector3.forward * (Input.GetAxis("Vertical") * playerSpeed * Time.fixedDeltaTime)); 

If you use a CharacterController, use Move or SimpleMove.

controller.Move(Vector3.forward * (Input.GetAxis("Vertical") * playerSpeed * Time.deltaTime));

Only use rigidbody.position if you want to teleport your rigidbody instead of moving it continuously.

Only use transform.position if you don't have a rigidbody, or want to teleport a character controller.


Rotations follow nearly the exact same rules. Scaling is however more complicated, and should be avoided for rigidbodies.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.