- Home /
I figured it out
How to change the direction the player is facing when colliding with an object
So I am still very new to unity and I am having trouble with some code I have that changes the direction the character model is facing so that it faces in the direction the player is moving. What I'm having trouble with in particular is that I have a reverse gravity zone and for whatever reason when the player enters it it reverses not only the gravity, but also the player's controls so I need to switch it back to normal. I figured out how to change the controls, but the character now faces the wrong way while moving and I have no clue how to fix it.
using UnityEngine;
using System.Collections;
public class Adjust2dFacing : MonoBehaviour
{
public CheckType inputType = CheckType.InputAxis;
public float axisThreshold = 0.2f;
public KeyCode inputKeyLeft = KeyCode.A;
public KeyCode inputKeyRight = KeyCode.D;
private bool isFacingRight = true;
public enum CheckType
{
InputAxis,
InputKeys
}
// Update is called once per frame
void Update ()
{
if(inputType == CheckType.InputAxis) // check axis input
{
float xInput = Input.GetAxis("Horizontal");
if(!isFacingRight && xInput > axisThreshold) // facing left - turn right
{
transform.Rotate(0, 180, 0);
}
else if(isFacingRight && xInput < -axisThreshold) // facing right - turn left
{
transform.Rotate(0, 180, 0);
}
}
else if(inputType == CheckType.InputKeys) // check key input
{
if(!isFacingRight && Input.GetKey(inputKeyRight)) // facing left - turn right
{
transform.Rotate(0, 180, 0);
}
else if(isFacingRight && Input.GetKey(inputKeyLeft)) // facing right - turn left
{
transform.Rotate(0, 180, 0);
}
}
// update facing direction
isFacingRight = (transform.position.x + (transform.forward * 5).x) > transform.position.x;
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Reverse")
{
inputKeyLeft = KeyCode.D;
inputKeyRight = KeyCode.A;
transform.Rotate(0, 180, 0);
}
}
}
Answer by tormentoarmagedoom · Sep 19, 2018 at 10:31 PM
Good day.
How are you moving the player? I think you are overthinking one simple problem.
You must know you can move and rotate objects in soo many ways. But in particular for this case, you must know you can use global axis, i mean static absloute axis of unity, or use the relative axis from the object. (when it rotates 180º, its relative axis rotates with it).
I think you are moving the player with its local rotation, but the camera is not rotating with it, so when object rotates, it becomes "reversed". the most easy way is to change the commands you use for moving.
Maybe this is better for you:
https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html https://docs.unity3d.com/ScriptReference/Transform-position.html
changing it directly, and use lerp to smooth the movement
Bye!
Oh ya I forgot some important information, the player is moving with a character controller and this is for a 2D platformer in unity 3D.
Answer by ahstein · Sep 20, 2018 at 12:52 AM
Is your character a sprite or 3d mesh?
If it is a sprite, are you aware of the flip property of the Sprite Renderer Component? If you just set flipX to true, you sprite will be mirrored along the y axis without having to worry about the weirdness of rotating the transform.