Okay so guys, this is what I’ve gotten so far. What I’ve got above is the code the allows the vehicle to be moved left and right using the arrow keys. However since I’m also porting to mobile i also need to have the ability to move left and right using the buttons in my canvas. Now I’ve setup the functions in my buttons as you can see, but no matter what code I try in these functions I can’t get my vehicle to move at all. Any ideas?
Or. If it’s possible to easily implement. Could i scrap the buttons and make it so that the car is moved left and right by the input of the players finger?? What should I look at if i want to do this?
Are you doing nothing a lane based system so there is 3 lanes and you switch on the press or gently move across each way. If the second one look into the Ipointerdata interfaces to get when you press down. You could also use the mobile input features to handle swiping if you’re are doing lane based
Edit: here’s the link to the interface , just write a script using this interface to get if a button is pressed instead of clicked link unites default
when you REALESE button function MoveLeft will be called IF IT IS PUBLIC. its hard to say here what isnt working
if want for player fingers you can check player input position on screen and if it is lower than screen.width/2 then move left otherwise move right (you can put that in any code so no canvas or other things)
So along the lines of if the player input is < that screen.width/2 then move left, else move right? Sounds easy enough but what would i put into the code to seperate left and right movement. The way I’m currently doing it whilst running the game on windows is check if the player is using the left or right arrow key on the horizontal axis, and this determines whether they move left or right. How could i do the same by using the input - screen.width/2 method??
you have unity specific sections like
#if UNITY_WINDOWS //or something like that
#if UNITY_ANDROID
#end
goes something like this so if its for windows set controls with arrows else on android check input and if is left and right and move in right direction (its same as input.getkey(keycode.leftarrow) but you check if(inputposition < screen.width/2))
But for the windows one I don’t use input.getkey(keycode.leftarrow) i use
position.x += Input.GetAxis (“Horizontal”) * carSpeed * Time.deltaTime;
position.x = Mathf.Clamp (position.x,-maxPos,maxPos);
transform.position = position;
Leaving me quite confused to to put after the if(inputposition < screen.width/2)) regarding to moving my vehicle left and right
Have you actually added the code to the MoveLeft() and MoveRight() functions? I know it’s a stupid question, but in your screenshot, you didnt have any functionality in the methods, hence why nothing was happening.
input get axis returns between -1 and 1 (on joystick and if keys are not set to instant can return floats like 0.5)
so you can keep this for windows but for android you can do
int axis;
if (fingerPos < screen.width/2)
{
axis = -1;
} else if (bigger than)
axis = 1;
}else
axis = 0;
}
after that copy code and replace that horizontal axis part with just axis.
I didn’t know what to add to them which was the problem :')
Well if you use left right side of screen you dont need buttons. Easiest way would be again with flags. Make moving in update function same as before but whole part of get axis horizontal replace with int axis. Int axis you set to -1 or 1 based on controls, in android case i already explained
Okay got this so far
if(fingerPos < Screen.width/2)
{
axis = -1;
}
else if (fingerPos > Screen.width/2)
{
axis = 1;
}
else
axis = 0;
However fingerPos doesn’t exist for me and I’m not sure what it’s supposed to be exactly?
heh
that is variable of finger position. i can just say what to do but seriously you would still have alot of questions. first do scavanger (the 2d tutorial of unity) and MANY questions will be answered.
I tried what I thought might be a simpler method of adding this code to my two UI buttons
public void MoveRight()
{
//Move player right
car.transform.Translate(Vector3.right * carSpeed * Time.deltaTime);
}
public void MoveLeft()
{
//Move player left
car.transform.Translate(Vector3.left * carSpeed * Time.deltaTime);
}
car is a GameObject and has been set in the inspector. Unfortunately this hasn’t been working either? Any simple way to get this method working?
are you already find the Solution??? atless i can tell you…, last comment on aug 5, 2015…, its already pass three year lol…, im late in this page.
