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...