Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Justin0025 · May 26, 2014 at 04:28 PM · distanceboundaryco-oprestrictmovement

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.

(Video)


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.

(Video)


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 ();
         }
     }
 }
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Vertical Bar showing distance travelled in metre. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges