Question by
Snooopy22 · Nov 01, 2021 at 12:11 PM ·
animationmovement script
Why is Nothing Moving?
I had it moving, but when I added a gun later, it stopped moving but still played the animation, even though I did not change the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Walk1
{
public class Walk : MonoBehaviour {
public Rigidbody rb;
public float speed;
public float t;
public bool Input, R, L, U, D;
public Animator WALKA;
private void start(){
rb = GetComponent<Rigidbody>();
t = 0;
Input = true;
}
void Update()
{
if (Input)
{
gameObject.GetComponent<Animator> ().Rebind ();
}
if(VirtualInputManager.Instance.MoveRight && VirtualInputManager.Instance.MoveLeft)
{
Input = true;
}
if (VirtualInputManager.Instance.MoveUp && VirtualInputManager.Instance.MoveDown)
{
Input = true;
}
else{
if (VirtualInputManager.Instance.MoveRight)
{
Input = false;
MR ();
}
else if(VirtualInputManager.Instance.MoveLeft)
{
ML ();
Input = false;
}
else {
if (VirtualInputManager.Instance.MoveUp)
{
Input = false;
MU ();
}
else {
if(VirtualInputManager.Instance.MoveDown)
{
MD ();
Input = false;
}
else
{
Input = true;
}
}
}
}
}
void ML ()
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
void MR ()
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
void MU ()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
void MD ()
{
transform.Translate(-Vector3.forward * speed * Time.deltaTime);
}
}
}
Comment
Some single-line micro-functions make code more simple to read and understand. These
MLetc. are definitely not among those. Inline them, otherwise you're just obfuscating the program.Work on your code formatting as this can obfuscate your program even further
void start ()is a typo you missed
Optimize code formatting for readability to prevent/catch bugs earlier
using System.Collections.Generic;
using UnityEngine;
public class WalkComponent : MonoBehaviour
{
public Rigidbody rb;
public float speed, t;
public bool Input;
public Animator WALKA;
void Start ()
{
if( rb==null ) rb = GetComponent<Rigidbody>();
t = 0;
Input = true;
}
void Update ()
{
if( Input )
{
gameObject.GetComponent<Animator>().Rebind();
}
if( VirtualInputManager.Instance.MoveRight && VirtualInputManager.Instance.MoveLeft )
{
Input = true;
}
if( VirtualInputManager.Instance.MoveUp && VirtualInputManager.Instance.MoveDown )
{
Input = true;
}
else
{
if( VirtualInputManager.Instance.MoveRight )
{
Input = false;
transform.Translate( Vector3.right * speed * Time.deltaTime );
}
else if( VirtualInputManager.Instance.MoveLeft )
{
transform.Translate( Vector3.left * speed * Time.deltaTime );
Input = false;
}
else
{
if( VirtualInputManager.Instance.MoveUp )
{
Input = false;
transform.Translate( Vector3.forward * speed * Time.deltaTime );
}
else
{
if( VirtualInputManager.Instance.MoveDown )
{
transform.Translate( -Vector3.forward * speed * Time.deltaTime );
Input = false;
}
else
{
Input = true;
}
}
}
}
}
}
Your answer