- Home /
Question by
DaviCostaNovais · Jan 27, 2021 at 04:38 PM ·
character
I can't get the character to turn around
The character doesn't turn around. *pressing the left button
Here is my script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour {
public float Speed;
public float JumpForce;
public bool isJumping;
public bool doubleJump;
private Rigidbody2D rig;
private Animator anim;
// Start is called before the first frame update
void Start()
{
rig = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Move();
Jump();
}
void Move()
{
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * Speed;
if(Input.GetAxis("Horizontal") > 0f)
anim.SetBool("Walk", true);
transform.eulerAngles = new Vector3(0f,0f,0f);
if(Input.GetAxis("Horizontal") < 0f)
anim.SetBool("Walk", true);
transform.eulerAngles = new Vector3(0f,180f,0f);
if(Input.GetAxis("Horizontal") == 0f)
anim.SetBool("Walk", false);
}
void Jump()
{
if(Input.GetButtonDown("Jump"))
{
if(!isJumping)
{
rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D.Impulse);
doubleJump = true;
anim.SetBool("Jump", true);
}
else
{
if(doubleJump)
{
rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D.Impulse);
doubleJump = false;
anim.SetBool("Jump", false);
}
}
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.layer == 8)
{
isJumping = false;
anim.SetBool("Jump", false);
}
}
void OnCollisionExit2D(Collision2D collision)
{
if(collision.gameObject.layer == 8)
{
isJumping = true;
}
}
}
If you can help I will be grateful.
aaa.png
(96.2 kB)
Comment
Best Answer
Answer by NPnpNPnpNP · Jan 27, 2021 at 05:00 PM
It seems that you forget some braces here :
if(Input.GetAxis("Horizontal") > 0f)
anim.SetBool("Walk", true);
transform.eulerAngles = new Vector3(0f,0f,0f);
if(Input.GetAxis("Horizontal") < 0f)
anim.SetBool("Walk", true);
transform.eulerAngles = new Vector3(0f,180f,0f);
It think that this will work :
if(Input.GetAxis("Horizontal") > 0f)
{
anim.SetBool("Walk", true);
transform.eulerAngles = new Vector3(0f,0f,0f);
}
if(Input.GetAxis("Horizontal") < 0f)
{
anim.SetBool("Walk", true);
transform.eulerAngles = new Vector3(0f,180f,0f);
}
Your answer
Follow this Question
Related Questions
counting seconds that character is idle. 1 Answer
To Make a Character 3 Answers
accessing charactercontroller 1 Answer
Understanding Prefabs + Player Spawn Points 2 Answers
swapping meshes 1 Answer