- Home /
Player Boundaries/Movement Restrain for Local Co-Op Game
Hello!
I'm currently working on a project that will be a 4-player co-op, top-down game. While most of the mechanics are working, I'm stumped on one important one; the player boundaries.
As of right now, I've got two characters roaming around the map. Problem is, the camera can zoom out continuously, creating a never ending distance between the two players. My goal is to lock the players into a certain distance, keeping the camera from zooming out too far.
In the first video, I show the players being able to move without any boundaries, all is fine. Just the problem I mentioned above. The small sphere determines the middle point between the two characters.
Once that was done, I tried to restrict the distance the players can travel. I did this by telling the players that they can move 5 units away from the sphere. If the players move farther than than the 5 units, they stop moving. That worked too... Until the players get stuck.
This video shows what I mean. As you can see, the character on the left walks towards the wall, and gets stuck. This is because the distance is greater than the 5 units I gave it.
This is where I need your help. Attached is my character controller script. I need to make it so the characters don't get stuck when reaching their max distance. I would love if the characters would be able to walk along this "invisible wall", without getting stuck, and being able to move back once they have reached their max distance.
import InControl;
//Handling
public var rotationSpeed : float = 450;
public var walkSpeed : float = 5;
public var runSpeed = 8;
//System
private var targetRotation : Quaternion;
public var doorCollision : boolean = false;
//Components
private var controller : CharacterController;
private var cam : Camera;
//Player Distance
var sphere : GameObject;
var player : GameObject;
function Start () {
//Input Detector
InputManager.Setup();
InputManager.AttachDevice( new UnityInputDevice( new TwinStickProfile() ) );
controller = GetComponent(CharacterController);
cam = Camera.main;
}
function Update () {
var playerDistance = Vector3.Distance(sphere.transform.position, player.transform.position);
Debug.Log("Player 1" +/+ playerDistance);
//Input Detector
InputManager.Update();
var player1 = InputManager.Devices[0];
var gameManager = GameObject.FindWithTag("GameManager").GetComponent(GameManager);
//Player 1 Control
if (playerDistance < 5){
var input = Vector3(player1.LeftStickX, 0, player1.LeftStickY);
if (input != Vector3.zero) {
targetRotation = Quaternion.LookRotation(input);
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y,targetRotation.eulerAngles.y,rotationSpeed * Time.deltaTime);
}
var motion : Vector3 = input;
motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1)?.7f:1;
motion *= (player1.Action2.Value == 1)?runSpeed:walkSpeed;
motion += Vector3.up * -8;
controller.Move(motion * Time.deltaTime);
if (doorCollision == true && player1.Action4.Value == 1){
Debug.Log("Going Down");
gameManager.loadNextLevel ();
}
}
}
Your answer
Follow this Question
Related Questions
C# Creating a 2d boundary 1 Answer
Return to Start Position 1 Answer
My look at script not working? 0 Answers
Checking the distance between an instantiated object and an existing object 1 Answer