- Home /
Other
How can I make it so the player can't move past a certain point?
I want my game coded so that when the player is moving and it is on certain coordinates, it can't go past that point. It will act like an invisible barrier. I want this because my game is coded so that there is a wall that surrounds the map and whenever an object with a Box Collider touches it, it deletes. So when a bullet is fired, it moves until it touches the wall and deletes. But the thing is that the player can move and hit this wall and it deletes the player. So I want to see if I can get it so the player can't move to the wall that deletes things. I do have a movement script for the player so I want to see if I can attach some code to the movement script to get it to work.
Answer by Andrajoso · Mar 08, 2021 at 11:37 PM
You could do as Jopa pointed above or scripting the wall collider so it only deletes some kind of GameObjects according to names, tags or whatever you want. I'd use "OnTriggerEnter" to do this.
Inside method OnCollisionEnter or trigger enter (depends what you have) try adding sth like this void OnTriggerEnter(Collider collider) { if(collider.gameObject.tag != "Player") { //delete } }
It should filter the deletion and omit the player.
I replaced the "Player" part with the tag name and it still has the error "error CS1012: Too many characters in character literal". This is the only error I'm getting so hopefully there is an easy fix.
Answer by Jopa-Przemyslaw · Mar 08, 2021 at 10:36 PM
Add collider to the player as well as the wall. You don't have to script it, let the engine handle it for you. The only thing you should script is adding an exception for a player not to be deleted.
This script could use OnTriggerEnter method and should look like this:
void OnTriggerEnter(Collider other)
{
if (other.gameobject.tag != "arrow")
{
//delete
}
}
And it won't remove this "arrow" For more read docs: link to OnTriggerEnter documentation.
You're trying to declare a method like a variable. It should have the return type 'void' before the method name.
Yea, ofc it should, just added that 'void' so that everyone understands it for sure.
Follow this Question
Related Questions
Why does my fps controller's motion get disabled after it collides with an enemy cube? 0 Answers
Copter movement 1 Answer
Trouble creating knockback when setting velocity on RigidBody2D 0 Answers
How to set NavMeshAgent to move acording to own gameObject.tranform.forward? 1 Answer
Help Rotating a Player 0 Answers