1
\$\begingroup\$

I've just learned that if I want to move an object so it acts depending of physics, I need to use rigidbody.velocity instead of transform.Translate. When I was using transform.Translate the box glided without problem. The only problem was that when it collided with a wall it began to jump and spin like crazy. But that problem is fixed now!

But when I added a rigidbody and used rigidbody.velocity, another problem stemmed: It doesn't glide. I tried using rigidbody.velocity and also, instead, I tried to create a Vector 3 movement, and at the end of the code I would write rb.AddForce (movement);. However, it still rotates, instead of gliding.

What's is generating this weird problem?

This is my updated code:

using UnityEngine;
using System.Collections;

public class MoveCharacter : MonoBehaviour {

    public float deltaMovement = 1f;
    public Rigidbody rb;
    // Use this for initialization
    void Start ()
    {
        rb = GetComponent<Rigidbody> ();
    }

    // Update is called once per frame
    void Update ()
    {
        Debug.Log (Time.deltaTime);
        Moving();
    }


    void Moving()
    {
        Vector3 movement = new Vector3(0, 0, 0);
        //Moves the character to where it needs.
        if (Input.GetKey (KeyCode.A)) {
            movement = new Vector3(deltaMovement, 0, 0);
        } else if (Input.GetKey (KeyCode.D)){
            movement = new Vector3 (-deltaMovement, 0, 0);
        }
        float yRotation = Camera.main.transform.eulerAngles.y;
        float movementX = Mathf.Sin ((yRotation * Mathf.PI) / 180) * deltaMovement;
        float movementZ = Mathf.Cos ((yRotation * Mathf.PI) / 180) * deltaMovement;

        if (Input.GetKey (KeyCode.W)) {
            movement = new Vector3 (movementX, 0, movementZ);
        } else if (Input.GetKey (KeyCode.S)){
            movement = new Vector3 (-movementX, 0, -movementZ);
        }
        rb.AddForce (movement);
    }
}

By the way, changing between Update() and FixedUpdate() didn't help...

\$\endgroup\$
  • \$\begingroup\$ Just a guess here as I don't use Unity, but to me it looks like the surface friction is preventing the movement. \$\endgroup\$ – Tyyppi_77 Sep 24 '16 at 11:16
2
\$\begingroup\$

Take into account that Applying a force is not the same as changing the velocity of the object.

Without friction, the result would be very similar, but when an object is standing in a surface, and there is friction between them, the behaviour could be very different.

In the videos you uploaded, the cube is rolling because you are pushing it from a side, just like a regular cube would roll if the friction was strong enough.

If you want the cube to glide as you say, you have to remove this friction. To do so you must assing a new Phycics material to your cube, and remove friction from it.

  • Create a Physics material

  • Select it, and in the inspector, remove friction:

  • Assign the new material to the Box Collider

Also, I would highly recommend update Physics behaviour from FixedUpdate()

As for the function to move the object, it should look something like this:

void FixedUpdate()
{

    //Moves the character to where it needs.
    if (Input.GetKey(KeyCode.D))
    {
        GetComponent<Rigidbody>().angularVelocity = Vector3.up * deltaMovement;
    }
    else if (Input.GetKey(KeyCode.A))
    {
        GetComponent<Rigidbody>().angularVelocity = -Vector3.up * deltaMovement;
    }

    if (Input.GetKey(KeyCode.W))
    {
        GetComponent<Rigidbody>().velocity = transform.forward * deltaMovement;
    }
    else if (Input.GetKey(KeyCode.S))
    {
        GetComponent<Rigidbody>().velocity = -transform.forward * deltaMovement;
    }

}

Notice I'm using velocity. If you were to use AddForce, the object would move faster and faster, as you keep applying force to the object.

If you decide to use velocity, you can rise the material friction a bit if you wish.

\$\endgroup\$
  • \$\begingroup\$ Should I leave the code as it is? \$\endgroup\$ – Pichi Wuana Sep 24 '16 at 15:26
  • \$\begingroup\$ Well, I don't see anything inherently wrong with it, beside updating physics on the Update function instead on FixedUpdate. It really depends on what you are trying to achieve. What kind of movement do you want to implement? WASD moving the object through each axis/direction, or do you want WS to move forward and backward, and rotate with A and D? \$\endgroup\$ – Leo Sep 24 '16 at 15:30
  • \$\begingroup\$ Moving the object through each direction. \$\endgroup\$ – Pichi Wuana Sep 24 '16 at 19:02
  • \$\begingroup\$ Moreover, I removed the friction and it still rotates on the z-axis. The difference is that it rotates on the edge and when it falls on another side of the cube, it glides on z-axis with WS as it should, but in x-axis inversely (D moves left). In addition, this would hinder me from rotating the camera as it should work. \$\endgroup\$ – Pichi Wuana Sep 24 '16 at 19:13
  • \$\begingroup\$ You want the cube to be able to rotate? You could freeze the rotation so the cube never rotates, no matter what happens. \$\endgroup\$ – Leo Sep 24 '16 at 21:37

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.