Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Jax1212 · Sep 30, 2018 at 03:40 PM · movementprogrammingtopdown

TopDown IfStatment Help

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CharacterContoller : MonoBehaviour
 {
 
     public float Speed = 0.1f;
     Transform Player;
 
     void Start ()
     {
         Player = GameObject.Find("Player").GetComponent<Transform> ();
     }
 
     void FixedUpdate ()
     {
 
         Vector3 Movement = Vector3.zero;
 
         if (Input.GetKey(KeyCode.W))
         {
             Movement = Vector3.up * Speed;
         }
         if (Input.GetKey(KeyCode.A))
         {
             Movement = Vector3.left * Speed;
         }
         if (Input.GetKey(KeyCode.S))
         {
             Movement = Vector3.down * Speed;
         }
         if (Input.GetKey(KeyCode.D))
         {
             Movement = Vector3.right * Speed;
         }
 
         Player.Translate (Movement);
     }
 }


The problem that I'm having is that these ifStatments are going in order. So for example, if I'm holding down W I can still press ASD to go in other directions. But if I'm Pressing like S I can only go to the right by pressing D because of the order of the if statements. I want to get it to where they all are able to go in all directions or so you have to release the key that you are holding down to go in a different direction. All Help appreciated <3,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Skokon · Sep 30, 2018 at 04:30 PM

You have to do it like this way

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class CharacterContoller : MonoBehaviour
  {
  
      public float Speed = 0.1f;
      Transform Player;
  
      void Start ()
      {
          Player = GameObject.Find("Player").GetComponent<Transform> ();
      }
  
      void FixedUpdate ()
      {
  
          Vector3 Movement = Vector3.zero;
  
          if (Input.GetKey(KeyCode.W))
          {
              Movement = Vector3.up * Speed;
          }
          else if (Input.GetKey(KeyCode.A))
          {
              Movement = Vector3.left * Speed;
          }
          else if (Input.GetKey(KeyCode.S))
          {
              Movement = Vector3.down * Speed;
          }
          else if (Input.GetKey(KeyCode.D))
          {
              Movement = Vector3.right * Speed;
          }
  
          Player.Translate (Movement);
      }
  }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jax1212 · Sep 30, 2018 at 04:49 PM 0
Share

@Skokon This Change Seems to just reverse the effect. $$anonymous$$aking it so when I am holding down the D $$anonymous$$ey Im able to Press SAW but if I'm holding down the S $$anonymous$$ey I Can Only Press AW.

avatar image Skokon Jax1212 · Sep 30, 2018 at 06:57 PM 0
Share

Assign Horizontal and Vertical input keys from Edit -> Project settings -> Input

and change your code to this

  using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
   
   public class CharacterContoller : $$anonymous$$onoBehaviour
   {
   
       float Speed = 0.1f;
       Transform Player;
       float inputHorizontal;
       float inputVertical;
   
       void Start ()
       {
           Player = GameObject.Find("Player").GetComponent<Transform> ();
       }
   
       void FixedUpdate ()
       {
           Vector3 $$anonymous$$ovement = Vector3.zero;
           inputHorizontal = Input.GetAxisRaw("Horizontal");
           inputVertical = Input.GetAxisRaw("Vertical");
           $$anonymous$$ovement = ( Player.right * inputHorizontal + Player.forward * inputVertical).normalized;
   
           Player.Translate ($$anonymous$$ovement);
       }
   }

avatar image Jax1212 Skokon · Sep 30, 2018 at 07:37 PM 0
Share

https://gyazo.com/ff6a4a548443794c8e56962a5e0e651b

I'm getting some errors. Don't I need to Include "Vector3 $$anonymous$$ovement = Vector3.zero;" ?

Show more comments
avatar image
0

Answer by Jan-Eylander · Sep 30, 2018 at 11:36 PM

If you want to stick to your code snippet you can just add a + infront of the = like this

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class CharacterContoller : MonoBehaviour
  {
  
      public float Speed = 0.1f;
      Transform Player;
  
      void Start ()
      {
          Player = GameObject.Find("Player").GetComponent<Transform> ();
      }
  
      void FixedUpdate ()
      {
  
          Vector3 Movement = Vector3.zero;
  
          if (Input.GetKey(KeyCode.W))
          {
              Movement += Vector3.up * Speed;
          }
          if (Input.GetKey(KeyCode.A))
          {
              Movement += Vector3.left * Speed;
          }
          if (Input.GetKey(KeyCode.S))
          {
              Movement += Vector3.down * Speed;
          }
          if (Input.GetKey(KeyCode.D))
          {
              Movement += Vector3.right * Speed;
          }
  
          Player.Translate (Movement);
      }
  }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

213 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object move, not teleport? 0 Answers

How can I stop my character from walking during the attack animation? 1 Answer

Trying to get camera to follow mouse, and player movement to be relative to camera angle? 0 Answers

Animator: multiple characters and one script 0 Answers

How to use hold/tap interactions in new Input System 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges