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 PuneetK · Sep 05, 2013 at 03:11 PM · movementaxiscuberestrictmovementmoves

Restricting axis of movement not working at all!

Hey people. I'm doing a board game on which the player (cube) can only move in directions where there is no obstacle. Kind of like pudding monsters game.

THE EXACT PROBLEM WITH THE CURRENT METHOD: 1. Lets say I am on the right corner of the board, and I move forward. Now, I am still on the right edge of the board, and there is an obstacle to my front. No of moves = 1. Now, I hit the right button (even though Im already on the edge and cant move to the right ideally), the player moves a tiny amount to the right, and the moves count increases. The next time I hit right, there is no issue. But NOW if I hit front, it will move a little to the front (almost invisible) but the moves count increases. What SHOULD happen is that on hitting right OR front, the moves count should NOT increase, and there should be no little movements.

What I'm implementing now is, 3 functions called in the update: drawrays() - this function is used to draw rays from all 4 sides of the player (cube). It calculates the distance between player and the object in each direction.

getInput() - this function gets the input either w,a,s, or d. Depending on the input, it moves in a certain direction.

DoMovement() - 4 if statements, for 4 movement directions. Depending on the variable, corresponding movement is done (via itween). Here, say if you put W (for forward), the boolean forward movement restriction gets true, rest are false.

Here is the exact code, which will help me explain better!

 using UnityEngine;
 using System.Collections;
 
 public class move7 : MonoBehaviour {
     
     GameObject player, prevpos, tile;
     bool forward = false, backward = false, left = false, right = false;
     bool moving = false;
     bool restrictF = false, restrictL = false, restrictB = false, restrictR = false;
     RaycastHit lefthitter, righthitter, fronthitter, backhitter, tilechecker;
     float L,R,F,B, movetime=0.5f;
     int moves=0, points=0, score=0;
     bool collectedbonus = false, levelcomplete = false;
     static int currlevel=0;
     Vector3 savedpos = new Vector3 (0,0,0);
     Transform tile2;
     public Texture floormaterial;
     int nooflevels = 6;
     
 /*---------------------------------------------------------------------------------------------------------------*/
     
     // Use this for initialization
     void Start () {
         player = GameObject.FindGameObjectWithTag("Player");
     }
     
 /*---------------------------------------------------------------------------------------------------------------*/
     
     // Update is called once per frame
     void FixedUpdate () 
 {
         drawrays ();    
         getInput(); 
 } //END OF UPDATE!
     
 
 /*---------------------------------------------------------------------------------------------------------------*/
     
     
 /*---------------------------------------------------------------------------------------------------------------*/
     
     void getInput()
     {
         if(Input.GetKey(KeyCode.W) && !restrictF)
         {
             forward = true;
             backward = false;
             left = false;
             right = false;
             moves++;
             restrictF = true;
             restrictL = true;
             restrictB = true;
             restrictR = true;
         }
         
         else if(Input.GetKey(KeyCode.A) && !restrictL)
         {
             left = true;
             forward = false;
             backward = false;
             right = false;
             moves++;
             restrictF = true;
             restrictB = true;
             restrictL = true;
             restrictR = true;
             
         }
         
         else if(Input.GetKey(KeyCode.S) && !restrictB){
             backward = true;
             forward = false;
             left = false;
             right = false;
             moves++;
             restrictF = true;
             restrictB = true;
             restrictL = true;
             restrictR = true;
             
             
         }
         
         else if(Input.GetKey(KeyCode.D) && !restrictR)
         {        
             right = true;
             forward = false;
             backward = false;
             left = false;
             moves++;
             restrictF = true;
             restrictB = true;
             restrictR = true;
             restrictL = true;
             
             
         }        
         
         doMovement(); //actually moves the playe
         
     }
         
 /*---------------------------------------------------------------------------------------------------------------*/
     
     void doMovement()
     {
         
         if(forward)
         {
             iTween.MoveAdd(player, iTween.Hash("z", F-0.5f, "time", movetime));
             restrictF=true;
             restrictL = false;
             restrictB = false;
             restrictR = false;
             forward=false;
             //print ("Allow L,R,F,B:" + restrictL + restrictR + restrictF + restrictB);
             
         }
         
         else if(backward)
         {
             //player.transform.Translate(0,0,(-B+0.5f));
             iTween.MoveAdd(player, iTween.Hash("z", -B+0.5f, "time", movetime));
             restrictB=true;
             restrictL = false;
             restrictF = false;
             restrictR = false;
             backward=false;
             //print ("Restrict L,R,F,B:" + restrictL + restrictR + restrictF + restrictB);
             
         }
         
         else if(left)
         {
             //player.transform.Translate(-L+0.5f,0,0);
             iTween.MoveAdd(player, iTween.Hash("x", -L+0.5f, "time", movetime));
             restrictL=true;
             restrictF = false;
             restrictB = false;
             restrictR = false;
             //print ("Allow L,R,F,B:" + restrictL + restrictR + restrictF + restrictB);
             
         }
         
         else if(right)
         {
             //player.transform.Translate(R-0.5f,0,0);
             iTween.MoveAdd(player, iTween.Hash("x", R-0.5f, "time", movetime));
             restrictR=true;
             restrictL = false;
             restrictB = false;
             restrictF = false;
             //print ("Allow L,R,F,B:" + restrictL + restrictR + restrictF + restrictB);
         }
         
     }
     
 /*---------------------------------------------------------------------------------------------------------------*/
 /*---------------------------------------------------------------------------------------------------------------*/
     
     void drawrays()
     {
                     
         if(Physics.Raycast(transform.position,Vector3.forward,out fronthitter))
          {
             F=fronthitter.distance;
          }//end of raycast checking forward wall
         if(Physics.Raycast(transform.position,Vector3.left,out lefthitter))
          {
              
             L=lefthitter.distance;
          }
         if(Physics.Raycast(transform.position,Vector3.right,out righthitter))
          {
             
             R=righthitter.distance;
          }
         if(Physics.Raycast(transform.position,-Vector3.forward,out backhitter))
          {
                 
             B=backhitter.distance;
          }
         
     }        
     
 }//END OF CODE

Please let me know if it's still not clear. I will try to explain more thoroughly.

Please do help, my other questions are still unanswered as well, if you could help me out there too I'd appreciate it :)

Comment
Add comment · Show 4
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 meat5000 ♦ · Sep 05, 2013 at 05:51 PM 0
Share

I notice in do$$anonymous$$ovement() you put

forward=false;

and

backward=false;

in the relevant if statements but you leave out left and right in their if statements.

Is this intentional?

avatar image PuneetK · Sep 05, 2013 at 06:22 PM 0
Share

No, I actually fixed that when I realised it. But it still didn't fix the issue :/

avatar image RyanZimmerman87 · Sep 07, 2013 at 08:45 AM 0
Share

This seems like a somewhat strange project to me in the way you are setting this up unless you are supposed to be moving through the tiles extremely fast or something like one tile every FixedUpdate() step?

For starters I would set up some kind of bools to control the logic to ensure things are executing properly.

$$anonymous$$aybe don't just call drawrays() and getInput() every fixedUpdate. Personally I would call drawrays() first. Then once it completes it should call getInput(). Assu$$anonymous$$g they can move in that direction then you call do$$anonymous$$ovement() if not then you continue to call getInput() until they enter something valid.

And you should have a control so that it only does the drawRays() after it does the do$$anonymous$$ovement().

So basically DrawRays -> getInput -> do$$anonymous$$ovement. It should always be executed in that exact order with no needless calls to ensure nothing breaks, and I haven't read your code thoroughly enough to know for sure but it seems that might not be the case here.

To be honest I'm not even sure if you need to use FixedUpdate() here you could just have all of this logic begin from an Update() GetButtonDown()

Once you press the button down it sets the ButtonDownBool to true and you can't press button down again until either the move completes or the button they press is not eligible for movement.

In short I think you need to take this a little more slow and careful and make sure all your logic is correct and don't call tons of raycasts and do so many things at once unless you are trying to have an extremely high speed non-stop race through this grid.

avatar image PuneetK · Sep 07, 2013 at 02:19 PM 0
Share

Well, the objective of my game is to have a player/cube that slides throughout the whole grid. So I need it to slide fast.

But I get what you're saying and have redone my code.

I have reduced my code a lot, and it is somewhat simpler, I'll show you the whole code. Also, yeah, the axis of movement has been restricted with the new framework. But I would just like your opinion on whether or not it is a correct framework for the type of game im making :)

     void Start () {
           player=GameObject.FindGameObjectWithTag("Player");
         prevpos = GameObject.Find("playerprevpos");
         currlevel=Application.loadedLevel;  
     }
     void FixedUpdate () 
     {
         checkTile ();
         getInput();
         hudUpdate();    
         checkLevelcomplete();        
 }
     void getInput()
     {
         if(prevpos.transform.position == transform.position)
         {
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W) && !restrictF)
         {
             restrictF = true;    
             forward = true;
             if(Physics.Raycast(transform.position,Vector3.forward,out fronthitter))
              {
                 F=fronthitter.distance;
                 FrontObj = fronthitter.collider.gameObject;
                 if(FrontObj.tag=="interactable_tile")
                  {
                        F=F+1f;    
                  }
             
                 
                 if(F >= 1f)
                     $$anonymous$$oveFront();
         }
             }            
         
         //same for other 3 directions
     }
         
 /*---------------------------------------------------------------------------------------------------------------*/
     
     void $$anonymous$$oveFront()
     {
             iTween.$$anonymous$$oveAdd(player, iTween.Hash("z", F-0.5f, "time", movetime));
             restrictF = true;
             restrictL = false;
             restrictB = false;
             restrictR = false;
             
             if(FrontObj.tag == "bomb" && forward)
              {
                     
                     forward = false;
                        Destroy (FrontObj,1);
                     restrictF = false;
              }
             moves++;
     }
     
 /*---------------------------------------------------------------------------------------------------------------*/
     
     void checkLevelcomplete()
     {
         //checking if the level is over
     }
     
 /*---------------------------------------------------------------------------------------------------------------*/
     
     void checkTile()
     {
         //check the tile below you and give result based on waht type of tile it is
     }    
     
 /*---------------------------------------------------------------------------------------------------------------*/
     
     void hudUpdate()
     {
         GameObject.Find("moves_HUD").guiText.text = "$$anonymous$$oves: " + moves;
         GameObject.Find("score_HUD").guiText.text = "Score: " + score;
         GameObject.Find("levels_HUD").guiText.text = "Level: " + (currlevel+1);
     }
 
     
 /*---------------------------------------------------------------------------------------------------------------*/    
     
     IEnumerator DoAnimation(GameObject hit)
     {
         
         //some animation
     }
     
 }//END OF CODE
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sainath Latkar · Sep 05, 2013 at 05:32 PM

I do not know but I am gettin similar issues. It is really irritating. I think the issue is with the values of the booleans u have set.

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

18 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

Related Questions

Direction of a mesh 2 Answers

Axis Issue 1 Answer

Katamari/RC car controls for dual analogue sticks 0 Answers

Z axis change 2 Answers

How to make 2D Movement with only 1 Axis at the same Time,How to make 2d Movement on 1 Axes at the same time? 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