- Home /
not allowing to walk on certain surfaces
Hi. maybe its a noob question but i can't figure out how to not allow my character walk on certain surfaces. Im creating a game with RTS like camera ( or something like unity shooter tutorial thing). I want to allow character to move on a red platform, and not allow to move for example on blue platform which is next to red one. The problem is I just cant add invisible walls, colliders etc. Can u give me some hints how to do it so i can figure out it by myself ( or if its too complicated for a noob scripter, can u post a whole script ) ? I thought of enabling movement script when colliding with red platform, and disabling when entering blue then bouncing back character a bit to red platform, or something like this. Just enabling and disabling mov. script is not working because player stops permamently
Thanks in advance :)
Please dont ask for completed scripts. People have been banned for this.
Answer by toddisarockstar · Apr 02, 2018 at 05:15 AM
GameObject g;
public GameObject guy;
public GameObject cam;
Vector3 tryit;
void Start () {
// this is to create an example level
//cam = GameObject.Find("Main Camera");
cam = Camera.main.gameObject;
guy = GameObject.CreatePrimitive(PrimitiveType.Cube);
guy.transform.position = new Vector3 (20, 1, 20);
// guy.transform.eulerAngles = new Vector3 (-10, 0, 0);
cam.transform.parent = guy.transform;
cam.transform.localPosition = new Vector3 (0, 6f, -10);
cam.transform.localEulerAngles = new Vector3 (10, 0, 0);
int i = 100;
while(i>0){i--;
int i2 = 100;
while(i2>0){i2--;
g = GameObject.CreatePrimitive(PrimitiveType.Cube);
g.transform.position = new Vector3(i,0,i2);
int i3 = Random.Range (0,10);
if(i3 == 0){g.transform.renderer.material.color = Color.red;}
else{g.transform.renderer.material.color = Color.blue;}
}}}
void FixedUpdate()
{ tryit = Vector3.zero;
if(Input.GetKey("w")){tryit = guy.transform.forward;}
if(Input.GetKey("s")){tryit = -guy.transform.forward;}
if(Input.GetKey("a")){guy.transform.Rotate(new Vector3(0,30*Time.deltaTime,0));}
if(Input.GetKey("d")){guy.transform.Rotate(new Vector3(0,-30*Time.deltaTime,0));}
tryit *= Time.deltaTime * 20;
tryit+=guy.transform.position;
// here is the part where you check the square ahead of yourself!!!
RaycastHit hit;
if(Physics.Raycast(tryit,-Vector3.up,out hit)){
if(hit.transform.renderer.material.color == Color.blue){
guy.transform.position = tryit;}
}
}
You should give an explanation with your script as its the idea you are trying to transmit. We are not a script requests service so it makes no sense handing over code without saying how it works. It encourages users to ask for completed scripts which is against the terms of the site.
Thanks for replies. Here how it looks. I have island with procedurally generated shape. It is surrounded with water. I want my character which im controlling to not move on water but only in island, but since thats procedurally generated i cant just add colliders and walls.I need hints so i can figure it by myself or scapes of code to implement in my game. That code from below is not working for me, sorry.
Answer by adamzl · Apr 02, 2018 at 04:09 AM
Mark the blue platforms as the notWalkable layer. https://docs.unity3d.com/Manual/nav-AreasAndCosts.html
Your answer
Follow this Question
Related Questions
Why are my bullets acting as being deflected by the camera borders collider? 0 Answers
Why isn't my move on collision script working? 0 Answers
How to stop a group of gameobjects from shaking when they arrive at their destination? 0 Answers
Collision with world object and object in Hand 0 Answers
Problem with 2D movement 0 Answers