- Home /
Question by
dinostudios123 · Jun 14, 2018 at 09:37 PM ·
c#gameobjectlocationgamepadswap
How to swap 2 game objects from any position on a 2d game pad?
Hi everyone. I am trying to change my script from swapping 2 gameobjects next to each other to swapping 2 gameobjects from any location.
I have a grid in my 2d match 3 game and I would like to swap any two gameobjects regardless of their location on the grid.
Here is my script in c#:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Berry.Utils;
// "Switch" Booster
// This object must be in the UI-panel of the booster. During activation (OnEnable) it turn a special mode of interaction with chips
public class BoosterSwitch : IBoosterLogic {
public Animation hand;
// Coroutine of special control mode
public override IEnumerator Logic ()
{
hand.gameObject.SetActive(false);
yield return StartCoroutine (Utils.WaitFor (SessionAssistant.main.CanIWait, 0.1f));
Chip chipA = null;
Chip chipB = null;
Side side = Side.Null;
System.Action<Chip, Side> fu = (Chip c, Side s) => {
if (c.slot && c.slot[s]) {
chipA = c;
chipB = c.slot[s].chip;
//location = l;
side = s;
}
};
ControlAssistant.swap = fu;
while (chipA == null || chipB == null)
yield return 0;
ProfileAssistant.main.local_profile["hand"]--;
ControlAssistant.swap = Chip.Swap;
Vector3 rotation = new Vector3();
switch (side) {
case Side.Null:
case Side.Bottom:
rotation.z = 0;
break;
case Side.Left:
rotation.z = -90;
break;
case Side.Top:
rotation.z = 180;
break;
case Side.Right:
rotation.z = 90;
break;
}
hand.gameObject.SetActive(true);
hand.transform.position = chipA.slot.transform.position;
hand.transform.eulerAngles = rotation;
hand.Play();
yield return new WaitForSeconds(0.5f);
SessionAssistant.main.SwapByPlayer(chipA, chipB, true);
SessionAssistant.main.swapEvent--;
SessionAssistant.main.movesCount ++;
while (hand.isPlaying)
yield return 0;
hand.gameObject.SetActive(false);
UIAssistant.main.ShowPage("Field");
}
public override void Disable() {
ControlAssistant.swap = Chip.Swap;
}
}
Any help would be great. Thanks
Comment
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
spawn an object at the location of an empty gameobject 1 Answer
How to swap 2 objects place with each other.(c#) 2 Answers
gameObject are not referenced 2 Answers