- Home /
Other
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.
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..
Follow this Question
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