Question by
camerondstanley · May 04, 2020 at 05:54 AM ·
rotationrigidbodyrotate
How do I get a Rigidbody to rotate in the direction it is moving?
I am trying to get a rigidbody to rotate toward the direction in which it is moving, but the character is rotating up and down fast on its own. I have tried several variations of transform.rotation = Quaternion.LookRotation(rb.velocity);
but nothing.
Here is my movement script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 2;
public Joystick joystick;
private Quaternion targetRotation;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
Vector3 current = transform.position;
Vector3 movement = new Vector3(joystick.Horizontal,0, joystick.Vertical);
rb.MovePosition(transform.position + (movement * moveSpeed * Time.deltaTime));
transform.rotation = Quaternion.LookRotation(rb.velocity);
}
}
Here is what is happening
It is a simple scene. My character and plane have rigidbodies and box colliders. I am using the Joystick pack by FENERAX STUDIOS and I have used a couple of packs from different people and I am getting the same problem. Sorry if wrong area to post or not enough info. Have not done this before. New to Unity
bobbing.gif
(254.6 kB)
Comment