- Home /
Comparing vector 3 locations
if(Input.GetButtonDown("up")){
//transform.position.currentPos.y = transform.position.currentPos.y+1;
currentPos.y = currentPos.y+1;
Debug.Log("currentPos"+ currentPos);
Debug.Log("startPos" +startPos);
if (currentPos.y != StartPos) {
Debug.Log("after currentPos.y!= startPos condional "+currentPos);
transform.position.y = transform.position.y+1;
currentPos= this.transform.position;
}
else if(currentPos == startPos){
Debug.Log("yes");
currentPos= this.transform.position;
}
}
I want to not allow my player to move up ones if the location they are moving to is the location of where startPos is.
Javascript
You just need transform.position.y, no need for currentPos.y.
But in your case, your code should still work. Except that on line 7, you typed "StartPos" when it's "startPos"
And what exactly is startPos? Is it a 3D position? If so, shouldn't you be limiting it for all 3 axis?
$$anonymous$$y fault the StartPos was my fault not proofreading it. The game is developed in 3d but the z axis never changes. $$anonymous$$y else if statement never gets caught even when my character is at (0,-1,0) and debug log still spits out (0,0,0,) which is where startCube is. Start cube is where the level starts and I dont want my character to be able to talk through it.
Answer by Yword · Aug 11, 2015 at 04:58 AM
Is that you want to move the object in grid but it cannot move to its starting position? If so, I had made some modifications based on your script, you may test it out to see whether it can help you or not.
#pragma strict
private var startPos : Vector3;
private var targetPos : Vector3;
function Start ()
{
// Record the startPos
startPos = transform.position;
}
function Update()
{
// Set targetPos as the transform's current position
targetPos = transform.position;
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
targetPos.x -= 1;
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
targetPos.x += 1;
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
targetPos.y += 1;
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
targetPos.y -= 1;
}
// Set transform at targetPos if it is not startPos
if (targetPos != startPos)
{
transform.position = targetPos;
}
}