Help with player rotation on rotating sphere
Hi,
I have a player object which I am able to move in a rotating sphere (a planet).
This works really well, I can move around the planet as I want to, horizontal input provide movement and vertical input provide rotation, so I press "A" and rotate left, then "W" to move forwards etc.
The problem I am having is that I don't want the player to have to rotate 180 to move and face in the opposite direction. Currently if the player moves backwards (pressing "S") the player will move backwards while still facing forwards. I would like, when the player inputs a backwards movement, for the player to rotate 180 and face their new direction too.
I'm not sure if the fact that my player is on a sphere which rotates is making this more complicated but all of the solutions I have tried haven't worked and have messed up the movement controls or the starting tranform position.
Any advice much appreciated.
CODE: using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 10f;
public float turnSpeed = 10f;
Transform myT;
private void Awake()
{
myT = transform;
}
void Update()
{
Move();
Turn();
}
void Turn()
{
float dir = turnSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
myT.Rotate(0, dir, 0);
}
void Move()
{
myT.position += myT.forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalP");
}
}
Answer by NoBrainer-David · May 13, 2017 at 04:26 PM
You could rotate the player-character anytime the user pressed down on the s key, like so:
if(Input.GetKeyDown("s"))
myT.Rotate(0, 180, 0);
Now, the character will rotate but then walk backwards in the old direction. You can change this by only getting the absolute value from the vertical axis like so:
Mathf.Abs(Input.GetAxis("Vertical"))
Thanks for the quick reply!
($$anonymous$$y mistake - it did work)
I'm sorry, it did actually work, my camera is set up so I couldn't actually see that it had worked (camera instantly flipped but because the player is a cube with one face standing on a uniform sphere I figured it was glitching!).
This works well, but I'm having a hard time making it work as I would like.
Currently, it will flip everytime "S" is pressed, I have tried to make it flip down only if it was facing up, and flip up (when pressing "W") only if it was facing down.
But it's not working, the player will not flip on "W" and will flip with every press of "S".
Here is what I tried.
void $$anonymous$$ove()
{
float updown = $$anonymous$$athf.Abs(Input.GetAxis("VerticalP"));
myT.position += myT.forward * moveSpeed * Time.deltaTime * updown;
if (facingUp = true && (Input.Get$$anonymous$$eyDown("s")))
myT.Rotate(0, 180, 0);
facingUp = false;
Debug.Log("Up");
if (facingUp = false && (Input.Get$$anonymous$$eyDown("w")))
myT.Rotate(0, 180, 0);
facingUp = true;
Debug.Log("Down");
}
The debug shows a continual output of Up/Down/Up/Down every frame.
Thanks for any help.
It's seems to me to be a problem with your braces. Use:
if (facingUp = true && (Input.Get$$anonymous$$eyDown("s")))
{
myT.Rotate(0, 180, 0);
facingUp = false;
Debug.Log("Up");
}
if (facingUp = false && (Input.Get$$anonymous$$eyDown("w")))
{
myT.Rotate(0, 180, 0);
facingUp = true;
Debug.Log("Down");
}
In C#, you use braces to create blocks of code. Only the next line is affected by the if-statement, unless you use braces to form a block of code, like I did above. This can be quite confusing, so some developers prefer to always use the braces for clarity.
Thanks, that makes sense!
I'm having a strange problem, in that when I debug facingUp, it's always false no matter what, and despite this, my "if faceingUp = true" is the only code which runs!
bool facingUp = true;
void Update()
{
$$anonymous$$ove();
Turn();
Debug.Log(facingUp);
}
void $$anonymous$$ove()
{
//float updown = $$anonymous$$athf.Abs(Input.GetAxis("VerticalP"));
myT.position += myT.forward * moveSpeed * Time.deltaTime * (Input.GetAxis("VerticalP"));
if (facingUp = true && (Input.Get$$anonymous$$eyDown("s")))
{
myT.Rotate(0, 180, 0);
facingUp = false;
Debug.Log("Down");
}
if (facingUp = false && (Input.Get$$anonymous$$eyDown("w")))
{
myT.Rotate(0, 180, 0);
facingUp = true;
Debug.Log("Up");
}
}
Debug will show "False" every step, and will show "Down" (and flip the player) whenever "S" is pressed (even once FacingUp is no longer true) but nothing will happen when pressing "W".
Can't seem to figure out what is wrong here.
Thanks for any help.
I am assu$$anonymous$$g that && in my if statement requires both conditions to return true before running the code, that is right, isn't it?