Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by kenwiggins93 · Jun 08, 2020 at 10:47 AM · scripting problemscripting beginnerdirectionvector2

Why doesn't my object move left?

So I have an object that isn't controlled by the player that I want to move left and right randomly at intervals. I wrote this script and it works somewhat except my object never moves to the right... I'm going mad trying to figure out why. I'm sure I'm missing something very obvious.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class OttoMoveAI : MonoBehaviour
 {
 
     public float moveSpeed = 100f;
     private Rigidbody2D rb;
     Animator anim;
     SpriteRenderer sP;
     public GameObject otto;
     bool moveRight;
     bool moveLeft;
     bool stayStill;
     private int randomNum;
     private int directionNumR;
     private int directionNumL;
     private Vector2 right;
     private Vector2 left;
    
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         anim = gameObject.GetComponent<Animator>();
         sP = gameObject.GetComponent<SpriteRenderer>();
 
         stayStill = true;
 
 
     }
 
 
 
 // Update is called once per frame
 void Update()
 
     {
        // directionNumR = Random.Range(0, 2);
        // directionNumL = Random.Range(0, -2);
 
         randomNum = Random.Range(1, 10);
 
         if (randomNum == 2)
         {
             moveRight = true;
             moveLeft = false;
             stayStill = false;
 
         }
 
         if
             (randomNum == 3)
         {
             moveRight = false;
             moveLeft = true;
             stayStill = false;
         }
             
         else
         {
             stayStill = true;
             anim.SetBool("isWalking", false);
         }
 
         if (moveRight && ! moveLeft  && ! stayStill)
         {
             rb.velocity = new Vector2(1 * moveSpeed, 0);
             anim.SetBool("isWalking", true);
             sP.flipX = true;
 
         }
 
         if (! moveRight &&  moveLeft && ! stayStill)
         {
             
             rb.velocity = new Vector2(-1 * moveSpeed, 0);
             anim.SetBool("isWalking", true);
             sP.flipX = false;
 
         }
       
     }
 }

Any ideas?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by N-8-D-e-v · Jun 08, 2020 at 06:04 PM

Well first off, you don't need to make two vector 2s for right and left, just use Vector2.right and -Vector2.right. Also, what your code is doing is checking if the random number is 2, then checking if it's three and if it's not it stays still, regardless of if the number was 2. So first change your code

          if (randomNum == 2)
          {
              moveRight = true;
              moveLeft = false;
              stayStill = false;
  
          }
  
          else if //make sure there's an else if
              (randomNum == 3)
          {
              moveRight = false;
              moveLeft = true;
              stayStill = false;
          }
              
          else
          {
              stayStill = true;
              anim.SetBool("isWalking", false);
          }

Next, try putting a physics material on the ground that has 0 friction. Then, another case where "else if" would be better, change your code

          if (moveRight && ! stayStill)
          {
              rb.velocity = new Vector2(moveSpeed, 0); //you don't need the * 1, as that just returns the same number
              anim.SetBool("isWalking", true);
              sP.flipX = true;
  
          }
  
          else if (moveLeft && ! stayStill) //add an else
          {
              
              rb.velocity = new Vector2(-1 * moveSpeed, 0);
              anim.SetBool("isWalking", true);
              sP.flipX = false;
  
          }

You should do this, as you can't have both options, if you have one you can't do the other. You can't be moving left and right at the same time

Hope this helps

Comment
Add comment · Show 2 · 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 kenwiggins93 · Jun 12, 2020 at 03:21 PM 0
Share

Hey, thanks so much! This works a charm.

avatar image N-8-D-e-v kenwiggins93 · Jun 18, 2020 at 08:25 PM 0
Share

Happy to help!

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

234 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 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 do I get the sound effect to continue playing? 1 Answer

How to increment value of axis when something happens 2 Answers

How to toggle on and off an animation with the same key? 1 Answer

How can I reset a sequence or order using Integers? 1 Answer

Iterate through area descibed by objects 1 Answer


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