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 /
This question was closed Dec 09, 2016 at 07:23 PM by Douglas-Elias for the following reason:

Other

avatar image
-1
Question by Douglas-Elias · Feb 06, 2014 at 03:32 PM · javascriptcharactercollider 2d

How to make a selectable platform?

I want to make the game from this anime: Phi Brain: Kami no Puzzle 2 - episode 06 (please watch this to understand what I trying to do), but I don't know how to make selectable platform. My project so far has a board and 12 platforms and when three platforms hit on each other they get back slowly, I don't now why they not stop when collide. I added this script to test the colliders (worked, they collide with the border and the platforms):

#pragma strict var moveUp: KeyCode; var moveDown: KeyCode; var moveLeft: KeyCode; var moveRight: KeyCode; var speed: float = 1.5; function Update () { if (Input.GetKey(moveUp)) { rigidbody2D.velocity.y = speed; } else if (Input.GetKey(moveDown)) { rigidbody2D.velocity.y = speed * -1; } else if (Input.GetKey(moveLeft)) { rigidbody2D.velocity.x = speed * -1; } else if (Input.GetKey(moveRight)) { rigidbody2D.velocity.x = speed; } /* else { rigidbody2D.velocity.y = 0; rigidbody2D.velocity.x = 0; }*/ }

I want that when is in my turn i can choose a platform, then choose the direction, so the character move from a platform to another ( the platform is a square and the charater is a circle), a script of all of this would be great.

If anyone could help with this I would appreciate.

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

1 Reply

  • Sort: 
avatar image
0

Answer by locrian05 · Feb 23, 2014 at 09:51 PM

try this.. though some restrictions are not yet set.. but selecting the platform and moving are all working..

Attach this to your player.I named that script "Script" important for variable calling. add a rigidbody2d component. set gravity to 0. also add a circle collider2d for trigger function. check is trigger.

 #pragma strict
 static var platformSelect : boolean;  //will be accessed by other script
 
 var moveUp: KeyCode;
 
 var moveDown: KeyCode;
 
 var moveLeft: KeyCode;
 
 var moveRight: KeyCode;
 
 var speed: float = 4;
 
 function Update () { 
 
     if(!platformSelect) {  //if platformSelect is false player can move
         if (Input.GetKey(moveUp)) { 
             rigidbody2D.velocity.y = speed; }
     
         else if (Input.GetKey(moveDown)) { 
             rigidbody2D.velocity.y = speed * -1; }
 
         else if (Input.GetKey(moveLeft)) { 
             rigidbody2D.velocity.x = speed * -1; }
 
         else if (Input.GetKey(moveRight)) {
             rigidbody2D.velocity.x = speed; }
 
         else { rigidbody2D.velocity.y = 0;
             rigidbody2D.velocity.x = 0; }
     }
     
 }


now for your cube "platform" add this script and make it as a prefab. also add a rigidbody2d, gravity 0. also add 2 boxcollider2d. check is trigger on 1 of the box. and check is kinetic on the other one.

 #pragma strict
 var select : KeyCode;  // i used spacebar for this key
 var onPlatform : boolean;
 var player : GameObject;  // i assigned it on the function start
 var platformSelect : boolean;
 var moveUp: KeyCode;
 
 var moveDown: KeyCode;
 
 var moveLeft: KeyCode;
 
 var moveRight: KeyCode;
 
 var speed: float = 4;
 
 function Start () {
     player = GameObject.Find("Player");
 }
 
 function Update () {   //onPlatform is triggered by onTrigger, see below script
     if(onPlatform && Input.GetKeyDown(select)) {
         Debug.Log("Selected");
         platformSelect = true;
         Script.platformSelect = true;  //affects the other script. Important
         onPlatform = false;
         rigidbody2D.isKinematic = false;
     }else if(!onPlatform && Input.GetKeyDown(select)) {
         Debug.Log("unSelect");
         platformSelect = false;
         Script.platformSelect = false; // enable players movement again
         rigidbody2D.isKinematic = true;  //will stop platform on collision
     }
     
         if(platformSelect) { //if true, player will follow platform
             player.transform.position = transform.position;
             //will move the platform on keypress
             if (Input.GetKey(moveUp)) { 
             rigidbody2D.velocity.y = speed; }
     
         else if (Input.GetKey(moveDown)) { 
             rigidbody2D.velocity.y = speed * -1; }
 
         else if (Input.GetKey(moveLeft)) { 
             rigidbody2D.velocity.x = speed * -1; }
 
         else if (Input.GetKey(moveRight)) {
             rigidbody2D.velocity.x = speed; }
 
         } //removed the else velocity 0 for the nonstop effect.
     }
 
 
 
 function OnTriggerEnter2D (hitInfo:Collider2D){
     if(hitInfo.name == "Player") {
         Debug.Log ("hit");
         onPlatform = true;
     }
 }
 function OnTriggerExit2D (hitInfo:Collider2D){
     if(hitInfo.name == "Player") {
         Debug.Log ("exit");
         onPlatform = false;
     }
 }


dont mind the debug.log, you can delete it, i just used it as reference. Hope this solves your problem..

Comment
Add comment · Share
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

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Problem with PlayerHealth and renderer 1 Answer

Sprint Error script? 2 Answers

Sprint Script Error?!!!! 4 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