Hello,
How can I get my character to flip the axis when facing the other direction?
Hello,
How can I get my character to flip the axis when facing the other direction?
How to report your problem productively in the Unity3D forums:
This is the bare minimum of information to report:
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: Using code tags properly
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[RequireComponent(typeof(Rigidbody2D))]
public class CController2D : MonoBehaviour
{
Rigidbody2D rigidbody2d;
[SerializeField] float speed = 2f;
Vector2 motionVector;
public Vector2 lastMotionVector;
Animator animator;
public bool moving;
public bool sprinting;
void Awake()
{
rigidbody2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
private void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
motionVector = new Vector2(
horizontal,
vertical
);
animator.SetFloat("horizontal", Input.GetAxisRaw("Horizontal"));
animator.SetFloat("vertical", Input.GetAxisRaw("Vertical"));
moving = horizontal != 0 || vertical != 0;
animator.SetBool("moving", moving);
if (horizontal != 0 || vertical != 0)
{
lastMotionVector = new Vector2(
horizontal,
vertical
).normalized;
animator.SetFloat("lastHorizontal", horizontal);
animator.SetFloat("lastVertical", vertical);
}
}
void FixedUpdate()
{
Move();
}
void Move()
{
rigidbody2d.velocity = motionVector * speed;
if (Input.GetKey(KeyCode.LeftShift))
{
rigidbody2d.velocity = motionVector * 2;
animator.Play("L_Sprinting");
}
}
}
What do I need to do?
Anyone? I’d really like to get this solved. Thank you!
Thank you. How should I modify my script for “Horizontal” to reflect an -x and x+ direction?
Just hold onto the last horizontal input the player put in. Then, if it’s less than zero, flip the x-axis (if your player is looking right by default, vice-versa otherwise).
Would you mind showing me the code revision? I’m sorry I’m new…
Btw you can also use a more simple method to flip the player.
void Update(){
if(horizontalnput > 0){
gameObject.transform.localScale = new Vector3(1,1,1)
}else if(horizontalInput<0){
gameObject.transform.localScale = new Vector3(-1,1,1)
}
}
Im sorry if it’s jancky im trying to help from my phone in math class.
It worked! Thank you!
I’ve got the player prefab transforms set to .5x .5y so I’ve converted it to a Vector2, and when I move the player transforms into 1, 1, 1. How can I reset transforms on the player prefab so that this script works?