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 /
This question was closed Mar 24, 2016 at 02:04 PM by CookieRobot for the following reason:

Fixed it myself

avatar image
0
Question by CookieRobot · Mar 24, 2016 at 05:35 AM · scripting problemmovement

Setting Velocities through Input

With my code as it stands here the "Left" command and animation work fine but the "Right" Command does not.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
     public float speed;
     private bool moveR;
     private bool moveL;
     private Animator animator;
     private Rigidbody2D rb;
     void Start()
     {
         animator = GetComponent<Animator>();
         rb = GetComponent<Rigidbody2D>();
         
 
     }
 
     void Update()
     { 
         bool right = Input.GetKey("right");
         bool left = Input.GetKey("left");
         if (right||left)
         {
             rb.velocity = new Vector2(0, 0);
         }
         
         
         if (right)
         {
             moveR = true;
             rb.velocity = new Vector2(speed, 0);
         }
         else
         {
             moveR = false;
             rb.velocity = new Vector2(0, 0);
         }
         animator.SetBool("runright", moveR);
         //
         if (left)
         {
             moveL = true;
             rb.velocity = new Vector2(-speed, 0);
         }
         else
         {
             moveL = false;
             rb.velocity = new Vector2(0, 0);
         }
         animator.SetBool("runleft", moveL);
 
     }
 }
 


My goal here is that I want the object to immediately be at a certain velocity upon pressing left or right then immediately stop when the button is released or if both are pressed at the same time. I do not understand why but only the "Left" input works as it stands. I am in 2D mode if that information helps. Thank you for your time.

Comment
Add comment · Show 1
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 CookieRobot · Mar 24, 2016 at 02:02 PM 0
Share

I figured it out. The issue is that you cannot have multiple "if" statements in a single cell. The fix was to just put "else" before every if statement after the first. Thanks for your help and time though.

1 Reply

  • Sort: 
avatar image
0

Answer by Abhiroop-Tandon · Mar 24, 2016 at 10:00 AM

It is a bit messed up but see if this works

  void Update()
      { 
          bool right = Input.GetKey("right");
          bool left = Input.GetKey("left");
          if (right && left)
          {
              rb.velocity = new Vector2(0, 0);
          }
          
          if (right)
          {
              moveR = true;
              rb.velocity = new Vector2(speed, 0);
          }         
          
          if (left)
          {
              moveL = true;
              rb.velocity = new Vector2(-speed, 0);
          }
          else
          {
              moveR = false;
              moveL = false;
              rb.velocity = new Vector2(0, 0);
          }
          animator.SetBool("runleft", moveL);
          animator.SetBool("runright", moveR);
  
      }

let me know if this works

Comment
Add comment · Show 1 · 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 CookieRobot · Mar 24, 2016 at 01:19 PM 0
Share

This does make it look a bit nicer but I'm still left with the same problem.

Follow this Question

Answers Answers and Comments

58 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

Related Questions

How can i detect a collision with many Terrains?, since my collider is not working? 0 Answers

Script to make hand move in a punching motion not working? 0 Answers

Help with simple player movement 0 Answers

Photon 2 Enemy AI Doesn't move? 1 Answer

how to dostraight ball throwing objects from returning? 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