- Home /
Is there a way to recapitulate this?
Hello I'm a newbie around, currently trying to understand how Unity and C# works while experimenting on different game prototypes. I found this piece of code's behaviour to my liking in a WASD controller in Isometric View, but it's almost too complicated for me to understand it. There are if statements everywhere and so on. It takes two objects (1 camera, 1 forward object) to move in a isometric view. I was wondering if there is a way doing that without that forward object (which i didn't understand why it is there) and recapitulate this code with a simple form. Here it is: public class Controller : MonoBehaviour {
     public Transform target;
     public float speed;
     public float runingSpeed;
     float currentSpeed;
     public GameObject GameCamera;
     public Transform objectforward;
     bool isRuning = false;
     Animator anim;
 
     void Start () {
         currentSpeed = speed;
         target.position = transform.position;
         anim = GetComponentInChildren<Animator>();
     }
     
     void Update() {
         Move();
         //target.position = new Vector3(transform.position.x + x, 0, transform.position.z + z) ;
     }
 
     void FixedUpdate(){
         float h = Input.GetAxisRaw("Horizontal");
         float v = Input.GetAxisRaw("Vertical");
         Animating(h, v);
         if (Input.GetKey(KeyCode.LeftShift)){
             bool running = h != 0f || v != 0f;
             anim.SetBool("IsRunning", running);
             anim.SetBool("IsWalking", false);
         }else{
             anim.SetBool("IsRunning", false);
         }
     }
 
     void Move(){
         float relativePosx = target.position.x - transform.position.x;
         float relativePosz = target.position.z - transform.position.z;
 
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
         if (x >= 1 || x <= -1 || z >= 1 || z <= -1){
 
             Quaternion rotation = Quaternion.LookRotation(new Vector3(relativePosx + 0.0001f, 0, relativePosz + 0.00001f));
             transform.rotation = rotation;
         }
         if (Input.GetKeyDown(KeyCode.LeftShift)){
             isRuning = true;
         }
         if (Input.GetKeyUp(KeyCode.LeftShift)){
             isRuning = false;
         }
         if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D)){
             transform.Translate((Vector3.forward * Time.deltaTime * currentSpeed));
         }
         if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D)){
             currentSpeed = 0;
         }
         if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.S)){
             currentSpeed = 0;
         }
         target.transform.rotation = GameCamera.transform.rotation;
         target.transform.eulerAngles = new Vector3(0, target.transform.eulerAngles.y, 0);
         if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)){
             if (isRuning == false){
                 currentSpeed = speed;
             }else{
                 currentSpeed = runingSpeed;
             }
             target.Translate(Vector3.forward * Time.deltaTime * currentSpeed * 4);
         }
         if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A)){
             if (isRuning == false){
                 currentSpeed = speed;
             }else{
                 currentSpeed = runingSpeed;
             }
             target.Translate(Vector3.right * Time.deltaTime * currentSpeed * 4);
         }
         if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W)){
             if (isRuning == false){
                 currentSpeed = speed;
             }else{
                 currentSpeed = runingSpeed;
             }
             target.Translate(Vector3.back * Time.deltaTime * currentSpeed * 4);
         }
         if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D)){
             if (isRuning == false){
                 currentSpeed = speed;
             }else{
                 currentSpeed = runingSpeed;
             }
             target.Translate(Vector3.left * Time.deltaTime * currentSpeed * 4);
         }
         if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.D)){
             target.transform.position = objectforward.transform.position;
             if (isRuning == false){
                 currentSpeed = speed;
             }else{
                 currentSpeed = runingSpeed;
             }
         }
     }
 
     void Animating(float h, float v){
         bool walking = h != 0f || v != 0f;
         anim.SetBool("IsWalking", walking);
     }
 }
 
Answer by UnityCoach · Jul 26, 2018 at 07:50 AM
I've had a quick look at your code. It's hard to say why you do all that, but here's a "clean" version, without all the ifs, misuse of FixedUpdate, mix of GetAxis and GetKey, etc.. It probably no longer does what you want, but it's a clean start, if this is what you want.
 public class Controller : MonoBehaviour
 {
     public Transform target;
 
     public float speed;
     public float runingSpeed;
     float currentSpeed;
 
     public GameObject GameCamera;
 
     bool isRuning = false;
     Animator anim;
 
     Vector3 direction;
 
     private bool _running;
     public bool running
     {
         get { return _running; }
         private set
         {
             if (value != _running)
             {
                 _running = value;
                 anim.SetBool("IsWalking", moving && !_running);
                 anim.SetBool("IsRunning", moving && _running);
 
                 currentSpeed = _running ? runingSpeed : speed;
             }
         }
     }
 
     private bool _moving;
     public bool moving
     {
         get { return _moving; }
         private set
         {
             if (value != _moving)
             {
                 _moving = value;
                 anim.SetBool("IsWalking", _moving && !running);
                 anim.SetBool("IsRunning", _moving && running);
             }
         }
     }
 
     void Start ()
     {
         currentSpeed = speed;
         target.position = transform.position;
         anim = GetComponentInChildren<Animator>();
     }
 
     void Update()
     {
         float relativePosx = target.position.x - transform.position.x;
         float relativePosz = target.position.z - transform.position.z;
 
         direction.x = Input.GetAxis("Horizontal");
         direction.z = Input.GetAxis("Vertical");
 
         moving = direction != Vector3.zero;
         running = Input.GetKey(KeyCode.LeftShift);
 
         transform.Translate((direction * Time.deltaTime * currentSpeed));
 
         target.transform.rotation = GameCamera.transform.rotation;
         target.transform.eulerAngles = new Vector3(0, target.transform.eulerAngles.y, 0);
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                