I’m making a game for iPhone/iPad. It’s a maze in kinda first person. I want the user to be able to walk around by clicking on arrows. When it’s possible to go left and right, I want both the left and right arrows able to click on. But when there’s only a possibility to go right, the only arrow which can be clicked will be the right one.
I made an example of what I want, because I don’t know if you could understand my explanation
I am going to assume you want some advice on how to do this. It should be quite simple.
Raycast is your friend for this task
Something like this:
float distanceToTest = 10;// How far from the wall will they be? Lets assume 10.
if( !Physics.Raycast( transform.position, transform.right, distanceToTest ) )
{
// We can go right
}
if( !Physics.Raycast( transform.position, -transform.right, distanceToTest ) ) // How far from the wall will they be? Lets assume 10.
{
// We can go left
}
if( !Physics.Raycast( transform.position, transform.forward, distanceToTest ) ) // How far from the wall will they be? Lets assume 10.
{
// We can go forward
}
The question indeed was how to make such a thing, sorry for not asking the question :P.
I tried that code, need I to put it in a JS or C#? I tried both, but didn’t work. It constantly gives me an error about the " ; "
It says: UCE0001: ‘;’ expected. Insert a semicolon on the end.
But that’s already there on that line. So I don’t get it.
And where do I add the script to, to all the walls, to the Person Controller?
I’m really sorry, you say it’s easy but I just don’t understand it :(…
Wow, thank you so much! That’s exactly what I wanted!
Hopefully I can get it to move to the left/forward/right by myself, I’m new to all this stuff, so it’s still difficult for me :).
Well, I tried really really hard, have been trying for hours, really, but I don’t get it to work when clicking on the text.
I did made something with the help of Google, and it’s kinda doing what I want, though it’s only for the right arrow.
But when I add this code to the Right arrow object, only the right arrow moves (but that’s kinda logic).
When I add it to the Capsule, the capsule moves as I want it when clicking on it.
But how do I say the Capsule has to move when I click the Right arrow object? (I hope you understand what I’m saying.)
Probably I’m doing things too difficult, cause I don’t understand much of it. If this wasn’t for a school project, I wouldn’t be doing this, haha :p.
This is the code I was talking about. It’s a JS. I tried adding things to your C# document, which I ought to do I guess, but I’m failing hard at understanding it.
var speed = 5;
var target : GameObject;
var test= false;
var number = 1;
var clicked=false;
var moveForward;
var turnRight;
var rotationAmount = 90.0;
function OnMouseDown() {
move();
}
function move()
{
turnRight = true;
yield;
turnRight = false;
moveForward = true;
yield;
moveForward = false;
}
function Update()
{
if(moveForward)
{
transform.Translate(0, 0, 5);
}
if (turnRight)
{
transform.Rotate (0, 45, 0);
}
}
At the moment you apply the transformation to the arrow because you use transform.translate. you want to apply it to the target by using target.transform.translate. it sounds like this is only going to get harder for you. I suggest you take an hour and go through some of the unity tutorials .