Question3r
Question3r

Reputation: 3752

Rotating Cube while walking on Sphere in Unity

I have a planet to walk on. I get attracted by the planet's gravity. The player automatically moves forward, you can just navigate him with A and D.

When I actually press A or D, I move to the left or to the right, but look still forward.

I want to rotate myself instead.

So when I keep one of the Buttons pressed this should happen

enter image description here

My current code looks this:

private void Update()
{
    moveDirection = new Vector3(data.InputHorizontal, 0, 1).normalized; // Always move forward and navigate with A and D
}

private void FixedUpdate()
{
    rigid.MovePosition(rigid.position + transform.TransformDirection(moveDirection) * 15 * Time.deltaTime); // Move the Player
}

So my problem is, that I don't know where to put my rotation code and what to write in there. Could someone help me out?

Upvotes: 0

Views: 439

Answers (1)

Umair M
Umair M

Reputation: 10720

You need to user input data to rotate the object and then move it in forward direction:

private void Update()
{
    moveDirection = new Vector3(data.InputHorizontal, 0, 1).normalized;
    tranform.Rotate(Vector3.up * data.InputHorizontal);
}

private void FixedUpdate()
{
    rigid.MovePosition(rigid.position + transform.forward  * 15 * Time.deltaTime);
}

hope this helps

Upvotes: 1

Related Questions