- Home /
Expand Object On Touch? Someone please help me
Hello. Part of my game needs a script that, when the object specified is touched, will expand the object on the X and Y axis. I made the following script hoping that it would work as I had wished, but unfortunately it doesn't work. I was hoping that posting this on here would let someone help me. But the past couple posts just don't get responses. The game is 2D and for the Android. Even if this seems quite easy to most of you, due to my lack of knowledge with Scripting and Game Creation it isn't for me, so all help is very appreciated.
pragma strict
public var localScale: Vector3;
function Update() {
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began)
transform.localScale += new Vector3(0.1F, 0, 0);
}
}
I'd just like to say. This is about my 3d or 4th time trying to receive an answer. I am in need of an answer to the problem as soon as possible. As said, all help is appreciated.
Answer by curiouspers · Mar 08, 2015 at 11:02 AM
Ok, you can use this method:
private bool selected = false;
private Vector3 defaultSize = Vector3.one;
public Vector3 selectedSize = new Vector3(0.1f, 0.1f, 0);
void OnMouseDown() {
Debug.Log("click down on "+name);
//if (GM.isGameOver || GM.isPaused) //in case you don't want to select when game is paused
// return;
selected = true;
}
void Update(){
if (selected) {
//sRenderer.color = new Color (1f, 1f, 1f, 0.5f); // if you need to change color
transform.localScale = defaultSize + selectedSize;
//col.size = defaultSize * 1f/transform.localScale; // if you need to keep your collider size
} else {
//sRenderer.color = new Color(1f,1f,1f,1f); // if you need to change color
transform.localScale = defaultSize;
//col.size = defaultSize;
}
}
It's C#, but i believe you can translate it to UnityScript if you want, because it's sooo similar. But you might want to use C#. Oh and don't forget to put collider on your object.
But i suggest you considering using mecanim animation for your objects.
EDIT: i looked up through your questions, and now i understand why you can't get answers for your questions and why you are not sattisfied with unityAnswers. It's because you post one question several times, up to 4 times, you really should not do this, and even when you get some answers you do not accept them as coorect, you do not upvote them, you do not comment back what is wrong, or what is right.
To be honest, you are not even rephrase your question, when it's clear that people don't get it right what you want to achieve, in one of four duplicates of this question, you posted this:
OnCollision is not what I want. I want it so that it continually expands when the user touches it once with his finger.
In this question, there is nothing about this. And this is sucks, because how do you get right answer if you do not ask correctly, and do not specify all the details you want?
Now my answer should be: Why are you not say that earlier? Replace Update function with this:
void Update(){
if (selected) {
transform.localScale += selectedSize;
} else {
transform.localScale = defaultSize;
}
}
...and you should be good to go. But i not sure that this is what you wanted.
And i got answer for you on how to get proper and fast answers:
As a first step i would suggest you watching this short video, that always been on the right side of this page.
Then try to EDIT your existing question, if you got any new details, instead of posting the duplicate, try to be as more specific as you can, add more details, add what you're already tried, what search you already made (and use search in first place before posting any question!), add pictures and videos if you have some, or even photo of your mockup on paper, if you think this will make thing simpler to understand. Because nobody knows what exactly do you have in your scene, or project, or your head. It's clear for you what you want to be done, so you must ask question in a way that it would be clear what you want to be done for anybody who reads your question.
Then go to your profile here on unityAnswers, by clicking on your nickname at the right top corner of this page, and follow this steps: Try to give a proper comment on all comments and answers you have. Try to upvote and select as correct answers when people was correct, and if they was not correct say them why they was not correct, maybe you're not provided some detail? Make this with every question you ever ask.
This will make people more friendly to you and you'll be good example for everybody, and as a bonus you will get answers more quickly! And maybe someday you can give good answers for others to share your knowledge!
I don't know if you have rights to delete or close duplicate questions, if you have the right to delete your own questions, delete every one of duplicate that has not been answered. This will make moderators life easier, and they will live thankfull and happy lives.
Then go to this page, and to tutorials section, if you follow at least some of them, great part of your questions will be solved, and you level up your skills several times for free!
On that same page you got documentation page that will vanish another part of your questions, and live training that is soooo awesome.
Then you
PS: i'm so so sorry for my poor english.
Thank you for responding. I will look into the links you have sent me. I've used the script you given, but it says this as an error. Assets/Standard Assets ($$anonymous$$obile)/Textures/Exapdn.cs(6,16): error CS0116: A namespace can only contain types and namespace declarations
I'd like to say that I have found a solution to this problem.
@SUPPEAR Glad to hear that, can you mark my answer as correct? It's right below thumbs down button.
I mean I could but the solution was one different from yours..