My character is a cube and the animation I’ve put on it makes it move forward but it online moves i one direction no matter what. How can i make it move left if you press left and move right if you press right?
Is there something wrong with the code?
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.EventSystems;
public class Players : MonoBehaviour
{
public Rigidbody player;
Animator anim;
public static float ySpeed = 0;
public static float xSpeed = 0;
public static float zSpeed = 0;
private void Awake()
{
}
private void Start()
{
anim = GetComponent<Animator>();
}
private void Update()
{
}
private void FixedUpdate()
{
player.velocity = new Vector3(xSpeed, ySpeed, zSpeed);
}
#region Movement
public void MoveLeft(string moveleft)
{
anim.SetBool("Idle", false);
anim.SetBool("Walk", true);
xSpeed = 3;
}
public void MoveRight(string moveright)
{
anim.SetBool("Idle", false);
anim.SetBool("Walk", true);
xSpeed = -3;
}
public void MoveForward(string moveforward)
{
anim.SetBool("Idle", false);
anim.SetBool("Walk", true);
zSpeed = -3;
}
public void MoveBackward(string movebackward)
{
anim.SetBool("Idle", false);
anim.SetBool("Walk", true);
zSpeed = 3;
}
public void StopMove(string stopmove)
{
anim.SetBool("Idle", true);
anim.SetBool("Walk", false);
zSpeed = 0;
xSpeed = 0;
}
public void OnPointerEnter(PointerEventData EventData)
{
}
public void OnPointerExit(PointerEventData EventData)
{
}
public void OnPointerDown(PointerEventData EventData)
{
}
public void OnPointerUp(PointerEventData EventData)
{
}
#endregion
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Lethal")
{
Destroy(player);
}
if (other.gameObject.tag == "Invincible")
{
Destroy(other.gameObject);
invincible = true;
}
}
}