Hello friends what i want is a little help running a for loop .
void Update ()
{
foreach (Touch touch in Input.touches) {
for()
{
for (start=(int)transform.position.x; start<=end; start++) {
Debug.Log ("well we entered dont know we r gonna exit or not");
transform.position = Vector3.Lerp (gameObject.transform.position, othercube.transform.position, Time.deltaTime);
start++;
}
}
}
the Blank for loop is the one i want like for(touch.phase==Began ;touch.phase==Ended) something along that lines ,I want a loop not an if condition .Please Help.
Just use the script example provided with the documentation. (The approach you take is documented as being bad for memory.)
I don’t actually understand what you tried to say but, i think the thing you look for is “While loop”.
while(touch.phase==Began)
{
//...do stuff while touch.phase equals to Began.
}
You have already added a foreach loop to read touches, you just need to check the phase and proceed with it:
foreach (Touch touch in Input.touches) {
if(touch.phase = TouchPhase.Began)
{
Debug.Log ("well we entered dont know we r gonna exit or not");
transform.position = Vector3.Lerp (gameObject.transform.position, othercube.transform.position, Time.deltaTime);
start++;
}
}
On every update it will do Lerp
if the phas eof the touch is Began
.
To be more specific with the touch specific handling you can capture the fingerId
of the touch and proceed based on fingerId
.
- Take a variable
int fingerId = -1
- Set
fingerId = touch.fingerId;
when phase is Began
.
- Reset to
-1
when phase is End
EDIT:
bool shouldLerp = false;
void Update ()
{
// Set variable to allow lerp when a touch senses.
If(Input.TouchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
shouldLerp = true;
}
// Variable is set when a touch senses. So, do Lerp.
if(shouldLerp) {
transform.position = Vector3.Lerp (gameObject.transform.position, othercube.transform.position, Time.deltaTime);
}
// Lerp termination condition. (EXAMPLE)
if(transform.position == Vector2.Zero)
{
shouldLerp = false;
}
}