what i have to do to make my character move up and down in the x Axis?
i want to make my character move in In all directions. what am I doing wrong?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour {
private Rigidbody myrigidbody;
[SerializeField]
private float movementspeed;
private bool Attack;
private bool Slide;
private bool facingRight;
private Animator myAnimator;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
private bool isGrounded;
private bool jump;
[SerializeField]
private float JumpForce;
public float Movementspeed
{
get
{
return movementspeed;
}
set
{
movementspeed = value;
}
}
void Start() {
facingRight = true;
myrigidbody = GetComponent<Rigidbody>();
myAnimator = GetComponent<Animator>();
}
void Update()
{
HandleInput();
}
// update is called once per frame
void FixedUpdate ()
{
float horizontal = Input.GetAxis("Horizontal");
isGrounded = IsGrounded();
flip(horizontal);
HandleMovement(horizontal);
HandleAttacks();
HandleLayers();
Resetvalues();
}
private void Resetvalues()
{
jump = false;
Attack = false;
Slide = false;
}
private void HandleAttacks()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
if (Attack && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
{
myAnimator.SetTrigger("Attack");
myrigidbody.velocity = Vector3.zero;
}
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
Attack = true;
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
Slide = true;
}
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
}
private void HandleMovement(float horizontal)
{
if (myrigidbody.velocity.y < 0)
{
myAnimator.SetBool ("land", true);
}
if (!myAnimator.GetBool("Slide") && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
{
myrigidbody.velocity = new Vector3(horizontal * Movementspeed, myrigidbody.velocity.y);
}
if (Slide && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
{
myAnimator.SetBool("Slide", true);
}
else if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
{
myAnimator.SetBool("Slide", false);
}
if (isGrounded && jump)
{
isGrounded = false;
myrigidbody.AddForce(new Vector3(0, JumpForce));
myAnimator.SetTrigger ("jump");
}
myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
}
private void flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
Vector3 theScale = transform.localScale;
theScale.x *= -1;
facingRight = !facingRight;
transform.localScale = theScale;
}
}
private bool IsGrounded()
{
if (myrigidbody.velocity.y <= 0)
foreach (Transform point in groundPoints)
{
Collider[] colliders = Physics.OverlapSphere(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
myAnimator.SetBool("land", false);
return true;
}
}
}
return false;
}
private void HandleLayers()
{
if (!IsGrounded()) {
myAnimator.SetLayerWeight (1, 1);
}
else
{
myAnimator.SetLayerWeight (1, 0);
}
}
}
capturar.png
(187.7 kB)
Comment
Your answer
Follow this Question
Related Questions
Cant Move towards Right and Jump at same time? 2 Answers
Stop movement on collision 1 Answer
Camera is Moving in Inspector but not in game? 1 Answer
I wrote a jumping script, but it isn't working and i'm not even getting any error codes. pls help. 0 Answers
How to make objects spawned from 2 players go in different directions. 0 Answers