Good Morning, could someone help me with any lines of code that would flip my character to face left the moment i pressed the left arrow so it would run to teh left? tried to find examples of it but due for what it uses i would like ot know if there would be any simple piece of coding to transform the character withn the space.world and leave it facing the pressed directionduring running and when its idle until its pressed the opposite direction
I heard some weird things can happen related to collision checking when you do negative scaling so I think itās not a great idea to change localScale.
I think it would be better to rotate the sprite 180°:
transform.eulerAngles = new Vector3(0, 0, 0); // Normal
transform.eulerAngles = new Vector3(0, 180, 0); // Flipped
using UnityEngine;
using System.Collections;
public class CharacterAnimatorScript : MonoBehaviour {
Animator anim;
bool isLeft;
bool isRight;
Vector3 X;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
X = transform.localScale;
isRight = true;
}
// Update is called once per frame
void Update () {
if(!isLeft (Input.GetKeyDown (KeyCode.A)||Input.GetKeyDown (KeyCode.LeftArrow))){
isRight=false;
isLeft=true;
// X.z=-X.z;
// transform.localScale = X;
transform.eulerAngles = new Vector3(0, -90, 0);
}
if(!isRight (Input.GetKeyDown (KeyCode.D)||Input.GetKeyDown (KeyCode.RightArrow))){
isRight=true;
isLeft=false;
// X.z=-X.z;
// transform.localScale = X;
transform.eulerAngles = new Vector3(0, 90, 0);
}
float move = Input.GetAxis ("Vertical");
if (move < 0) {
move=-move;
}
anim.SetFloat ("Speed", move);
bool jumping = anim.GetCurrentAnimatorStateInfo(0).IsName("Jump");
if (!jumping) {
if (Input.GetKeyDown (KeyCode.Space)) {
anim.SetBool ("Jump", true);
}
}
}
}
Currently this is the code for changing the axis, tried with checking the link u showed me but didnt went much foward being that it resembled mine i tried adding that line but still nothing :\ (using a 3d character thus i rotated it and using the vertical so i have him move on the X)
I donāt know, I heard people having trouble in the OnCollisionEnter/OnCollisionStay/OnCollisionExit functions when using -1 scaling but I might be wrong.
I also heard that in the next Unity update there should be an option for flipping your sprite
I know what you mean, here is how I have fixed that before:
if (Input.GetKey (KeyCode.D))
{
transform.Translate (Vector2.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2(0,0);
}
//Move Left
if (Input.GetKey (KeyCode.A))
{
transform.Translate (Vector2.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2(0,180); //flip the character on its x axis
}
I kept the player sprite set to move right on the left direction key, then flipping it forced it to move left. Hope that makes sense.
Ignoring my speed variable, you could probably use the same principle (guessing) as me in your code?
From reading your code and reading your problem in the first post I assume the character is facing right, when you press the D key it will stay looking right. When you press the left it still faces right but Iām not sure what part of the script is supposed to be making it actually move? Youād need a transform.Translate in there.
What I did was make both A+D move the character to the right, but on the transform.eulerAngles part for the āAā key I told Unity to flip my character 180 on the x axis which then automatically made it go left.
Probably not the best method, and Iām sure someone else will explain a better way - but it worked for me!
check out the 2D Character Controllers Video Tutorial from Unity. About minute 40 the Tutor implement what you want to do. Iam using this in my Plattformer also and it works fine for me.
Gave it a watch and tried it issue is with rigidbody (taking off character controller/motor and fps input scripts) doesnt work tough the animations arent triggering
can some one help me figure out why my āflip playerā wont work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovment : MonoBehaviour
{
public float speed;
private Vector2 direction;
public bool facingRight = true;
// Start is called before the first frame update
void Start()
{
direction = Vector2.up;
}
// Update is called once per frame
void Update()
{
GetInput();
Move();
}
public void Move()
{
transform.Translate(direction*speed*Time.deltaTime);
}
private void GetInput()
{
direction = Vector2.zero;
if (Input.GetKey(KeyCode.W))
{
direction += Vector2.up;
}
else if (Input.GetKey(KeyCode.S))
{
direction += Vector2.down;
}
if (Input.GetKey(KeyCode.A))
{
direction += Vector2.left;
}
else if (Input.GetKey(KeyCode.D))
{
direction += Vector2.right;
}
}
void FlipPlayer()
{
facingRight = !facingRight;
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
}
}