0
\$\begingroup\$

I'm trying to move around a 3D rigidbody in Unity 2018.1.0f2 Personal. I am using a C# script with the basic vector3 and velocity stuff from a YouTube video:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {
    [SerializeField]
    private Rigidbody playerBody;

    private Vector3 inputVector;

    private float xMovement;
    private float zMovement;

    [SerializeField]
    private float playerSpeed;

    private float maxSpeed;

    // Use this for initialization
    void Start () {
        playerBody = GetComponent<Rigidbody>();
        playerSpeed = 1;

    }

    // Update is called once per frame
    void Update () {
        xMovement = Input.GetAxisRaw("Horizontal")* playerSpeed;
        zMovement = Input.GetAxisRaw("Vertical") * playerSpeed;

        inputVector = new Vector3(xMovement, playerBody.velocity.y, zMovement);
        playerBody.velocity = inputVector;
    }
}

It works great with a cube I put it on, except for this: if I move the rigidbody and then stop, it takes a long time for it rotate from go only one of the edges or corners just touching the ground (screenshot of what I mean, another screenshot) to resting flat. How can I make it fall back to stability faster?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Try using Drag Property of RigidBody Component, Drag can be used to slow down an object. The higher the drag the more the object slows down. \$\endgroup\$ Apr 15 '19 at 6:14
  • \$\begingroup\$ It's not really clear from your screenshots what your problem is. A short video or animated gif would be more helpful. \$\endgroup\$
    – Philipp
    Apr 15 '19 at 13:58
1
\$\begingroup\$

From my understanding, it seems like you are updating the x and z rotation of the player inside the update method. so, it's setting the velocity each frame with the player movement. So stopping the player might causing that slow rigidbody.

So either you should manually handle the rotational movement along with the ground movement, or simply use Relative Torque to add rotational force to your player. Here is how my output looks like.

https://imgur.com/gallery/qljGJgJ

This is the change I made inside your code. I am not taking any consideration of y-axis movement and adding relative torque according to the input.

    inputVector = new Vector3(xMovement, 0, zMovement);
    playerBody.AddRelativeTorque(inputVector * playerSpeed,ForceMode.VelocityChange);

Hope that helps.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you! This works great! \$\endgroup\$ Aug 1 '19 at 18:39

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.