Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 ITSLEO · Sep 01, 2019 at 07:21 PM · movement3dmovement scriptaxismovements

Make the Player unable to move to opposite direction

Hello everyone, i am making a game that has a classic "Snake" game style movement, and for this reason, i need to make the player unable to go to the left if he previously went right, same thing for Up/Down. I tried setting up some booleans but it didn't work well, so i don't know how to make this work, if anyone could help me, i'd be really grateful ;) (note: the game is in 3D if is a needed piece of info)

This is my code, don't get scared by the 70+ lines, i write really clear and simple code ;)

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Movement : MonoBehaviour { public int SnakeSpeed = 100;

 // Start is called before the first frame update
 void Start()
 {
     StartCoroutine(StartGame());        
 }


 IEnumerator StartGame()
 {
     yield return new WaitForSeconds(3);
     InvokeRepeating("Going", 2, 1);
 }

 // Update is called once per frame
 void Update()
 {
     
     if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         Invoke("Right",1);           
     }

     if (Input.GetKeyDown(KeyCode.LeftArrow))
     {          
         Invoke("Left", 1);           
     }

     if (Input.GetKeyDown(KeyCode.DownArrow))
     {            
             Invoke("Down", 1);              
     }

     if (Input.GetKeyDown(KeyCode.UpArrow))
     {                       
             Invoke("Up", 1);                
     }
 }

 void Going()
 {
     transform.Translate(0, 0, SnakeSpeed * Time.deltaTime);
 }

 void Right()
 {
     transform.localEulerAngles = new Vector3(0, 90,0);
 }

 void Left()
 {
     transform.localEulerAngles = new Vector3(0, -90, 0);
 }

 void Up()
 {
     transform.localEulerAngles = new Vector3(0, 0, 0);
 }

 void Down()
 {
     transform.localEulerAngles = new Vector3(0, -180, 0);
 }

}

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

Answer by Sgt_Spike · Sep 01, 2019 at 09:49 PM

Hey, try using a string for this instead of multiple booleans. This code should work.

 public int snakeSpeed = 100;
     private string currentDir;
 
     void Start()
     {
         currentDir = "u";
         StartCoroutine(StartGame());
     }
     IEnumerator StartGame()
     {
         yield return new WaitForSeconds(3);
         InvokeRepeating("Going", 2, 1);
     }
 
     void Update()
     {
 
         if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             if (currentDir != "l")
             {
                 Invoke("Right", 1);
                 currentDir = "r";
             }
         }
         if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             if(currentDir != "r")
             {
                 Invoke("Left", 1);
                 currentDir = "l";
             }
         }
         if (Input.GetKeyDown(KeyCode.DownArrow))
         {
             if(currentDir != "u")
             {
                 Invoke("Down", 1);
                 currentDir = "d";
             }
         }
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             if (currentDir != "d")
             {
                 Invoke("Up", 1);
                 currentDir = "u";
             }
         }
     }
     void Going()
     {
         transform.Translate(0, 0, snakeSpeed * Time.deltaTime);
     }
     void Right()
     {
         transform.localEulerAngles = new Vector3(0, 90, 0);
     }
     void Left()
     {
         transform.localEulerAngles = new Vector3(0, -90, 0);
     }
     void Up()
     {
         transform.localEulerAngles = new Vector3(0, 0, 0);
     }
     void Down()
     {
         transform.localEulerAngles = new Vector3(0, -180, 0);
     }

The new string will detect which way the player last moved, either u, d, r, or l. When the player moves in a direction, the new if statement will use the string variable to make sure it's not the opposite direction, and if it is the opposite direction, the code will simply not execute. Hopefully it works!

(Also just as a friendly tip, try to name variables with a lowercase letter first, and every other new word for the variable will start with a capital letter, like I have done in the example code. It just makes them easier to read and stops you getting them mixed up with functions.)

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 ITSLEO · Sep 02, 2019 at 07:01 AM 0
Share

Thank you really much! It worked flawlessly! I'd give you reward points but i don't have any unfortunatly. I actually didn't think about the string mechanic, cool move ;)

avatar image Sgt_Spike ITSLEO · Sep 02, 2019 at 10:23 AM 0
Share

No worries, glad it helped. And strings can often be replaced with booleans when you have more than two choices. It just saves space rather than using like four different booleans. :D

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

260 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 avoid jittering / stuttering when colliding with objects in 3D? 2 Answers

3D space movement: character stuck at edge in free space 0 Answers

Can't Jump While Wall Running 0 Answers

Jumping in my script deosnt work,How make this script to jump? 0 Answers

Help changing input to axis 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