This didn't use to happen, but I haven't been working on my game for a few weeks. Today I was finally done with schoolwork so I was looking forward to working on the game. However, when I ran it to make sure everything was still OK, my character started shaking when moving diagonally.
How can I fix this?
I'm totally new to Unity, btw, and coding too.
This is what I've got in the Visual Studio
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour {
private Rigidbody2D myRigidbody;
public Animator myAnimator;
public float moveSpeed;
private bool facingRight;
// Use this for initialization
void Start () {
myRigidbody = GetComponent ();
facingRight = true;
myAnimator = GetComponent ();
}
// Update is called once per frame
void Update () {
if(Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") <-0.5f)
{
transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
}
}
void FixedUpdate()
{
float horizontal = Input.GetAxisRaw("Horizontal");
HandleMovement(horizontal);
Flip(horizontal);
}
private void HandleMovement(float horizontal)
{
myRigidbody.velocity = new Vector2(horizontal * moveSpeed, myRigidbody.velocity.y);
myAnimator.SetFloat("MoveX", Mathf.Abs(horizontal));
}
private void Flip(float horizontal)
{
if(horizontal<0 && facingRight || horizontal>0 && !facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}
Thank you! Any sort of help is highly appreciated!
↧