- Home /
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 :)
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?
No, I actually fixed that when I realised it. But it still didn't fix the issue :/
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.
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
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113630im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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