Hold touch button Unity Javascript
I have script for crouching when i press and hold S its crouching when i release the button the player gets in his normal size. Now i want to do the same thing with touch button but i cant figure how to do that, can some1 help?
static var vScale = 1.0;
private var tr: Transform;
private var dist: float; // distance to ground
function Start(){
tr = transform;
var ch:CharacterController = GetComponent(CharacterController);
dist = ch.height/2; // calculate distance to ground
}
function Update()
{
vScale = 1.0;s
if (Input.GetKey("s")){ // press s to crouch
vScale = 0.5;
}
var ultScale = tr.localScale.y; // crouch/stand up smoothly
tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position
}
function OnGUI ()
{
}
Could you please clarify "But i have cant add touch button ins$$anonymous$$ how to make Hold touch button?"
Do you want to support mobile touch input? Or toggle crouch when tapping S? Or handle a response from a UI button click? Or something else?
Oh i started typing 1 thing then another.. i want it to support mobile touch input i have the script i want and its good for me, when im holding S its crouching, I want to make the game to do the same thing with touch button. Btw i changed my text.
Answer by Statement · Oct 22, 2015 at 06:38 PM
Downloadable project (history, code)
Refactor your Update code to call functions Crouch
and Stand
. To make sure your script works like before, use Input.GetKeyDown and Input.GetKeyUp. (See code changes)
Then add a Button to your scene. Since my example has Crouch and Stand functions, I add an EventTrigger to the button and hook up Pointer Down and Pointer Up events to route to Crouch and Stand. The setup on the button look like this:
When you touch or press the mouse, the following happens.
Red arrow: EventSystem send events to button and is intercepted by EventTrigger.
Green dotted line: EventTrigger has references to the Crouch Example game object.
Edit: Ignore the green dotted line to the script. It just means that the script sits on that game object...
Blue arrow: EventTrigger routes Pointer Down and Pointer Up events to Crouch and Stand.
You mean a UI button?
If you want to get an event both when the user put their finger on and off the button, try adding an EventTrigger to the button and route the calls from those callbacks ins$$anonymous$$d.
Tnx, but 1 more thing can rewrite the code with crouch function? please
Dude i love u this is very useful I cant express my gratitude :D <3
1 more question for u sir :D this one is little off topic, this is my old test game http://i.imgur.com/jHIjEIq.png?1 and this is from the youtube tutorial http://i.imgur.com/w$$anonymous$$wW39z.png, why the view is different, we have the same code for camera, his camera has view on 3 sides of the platform while $$anonymous$$e has just 2. How can i fix this?
Your answer
Follow this Question
Related Questions
How to get touch's positon when I hold the button?? 2 Answers
Why is my multi touch not working? 1 Answer
Touch on 2D gameobject: should i use a GUI button? 1 Answer
UI button events not working on android 3 Answers
When The Screen Is Touched Jump 0 Answers