- Home /
Transform.LocalScale Script Problem. Please help.
Hello. I'd like to start off by saying that I am very new to Script and creating games, so all help is appreciated. I am trying to create a script that, when a certain object is touched, all objects within the folder would expand (Transform.LocalScale). I can't even figure out how to simply expand an object when it is touched. Just to clarify, when I say expand I mean that when the object is touched once it would continually expand. All help is appreciated.
Answer by Kowalski042 · Mar 01, 2015 at 07:38 AM
There are tutorials on Unity3d. Search for how to do OnCollision in Unity. That should help you.
OnCollision is not what I want. I want it so that it continually expands when the user touches it once with his finger.
Answer by skylem · Mar 01, 2015 at 05:57 AM
You've not provided the language in which your attempting this, heres a c# solution. heres a rough idea of how to achieve this, firstly your going to want a method to detect whether the object has been clicked i think the simplest way of doing this is with MouseOver/MouseExit functions
the following script should be placed upon the object your clicking
using UnityEngine;
using System.Collections;
public class AddUnitToSelection : MonoBehaviour {
public bool hover;
GameObject gameController;
// Use this for initialization
void Start () {
gameController = GameObject.FindGameObjectWithTag ("GameController");
}
// Update is called once per frame
void Update () {
if(hover && Input.GetButtonDown("Fire1")) {
gameController.GetComponent<SelectedUnit>().SetUnit(gameObject.transform);
}
}
void OnMouseOver() {
hover = true;
}
void OnMouseExit() {
hover = false;
}
}
this script can be placed anywhere in the scene but be sure to Tag the object as GameController.
using UnityEngine;
using System.Collections;
public class SelectedUnit : MonoBehaviour {
public Transform SelectedUnit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// here is where we will set the scale of the object we clicked before removing the selection
if(SelectedUnit != null) {
SelectedUnit.localScale = SelectedUnit.localScale * 2; // double the size
SelectedUnit = null; // Empty the selection
}
}
// this is where set the selected unit based off the object we clicked, Object we clicked
// Must have a Collider aswell as the first script attatched to it.
public void SetUnit(Transform tr) {
SelectedUnit = tr;
}
}
if you need more information on how they work just ask.
First off, I thank you for replying. I have quickly skimmed through and wanted to make sure that this would work on the android, as I am making it on the android.
certainly it would however u would need to substitute the Input.GetButton for a Touch Input, see the documentation on Input.GetTouch