- Home /
How to apply Time.deltaTime here?
It makes me more touch delay because of this. So help me how to reduce touch delay? If time.deltatime is working then where can i put it?
if (Application.isMobilePlatform)
{
if (Input.touchCount > 0)
{
for (int t = 0; t < Input.touchCount; t++)
{
Touch touch = Input.GetTouch(t);
TouchPhase phase = touch.phase;
if (phase == TouchPhase.Began)
{
aimTouchPosition = Camera.main.ScreenToWorldPoint(touch.position);
Shoot(aimTouchPosition);
}
}
}
}
}
public void Shoot(Vector3 aimPosition)
{
RaycastHit2D raycastHit2D = Physics2D.Raycast(aimPosition, Vector2.zero);
if (raycastHit2D.collider != null)
{
if (raycastHit2D.collider.tag == "Enemy")
{
raycastHit2D.collider.gameObject.GetComponent<CSGTEnemy>().hit();
}
}
}
Looks like you need to set Application.targetFrameRate = 60;
.
Actually not working because there's still little bit delay
Answer by tahir_ali · Jan 19, 2018 at 10:52 AM
Hey @MrCrumbl3d i think this line making the delay Camera.main.ScreenToWorldPoint(touch.position);
Because "Camera.main" every time find the camera having MainCamera tag and Finding something in Update function is expensive........... So I tried to optimize your code here it is..............
Camera cam;
void Start()
{
cam = Camera.main;
}
if (Application.isMobilePlatform)
{
int total_touches = Input.touchCount;
if (total_touches > 0)
{
Touch[] touches = Input.touches;
for (int t = 0; t < total_touches; t++)
{
if(touches[t].phase == TouchPhase.Began)
{
aimTouchPosition = cam.ScreenToWorldPoint(touch.position);
Shoot(aimTouchPosition);
}
}
}
}
I hope it helps...............
@tahir_ali still not working bro there's still a delay. Help please... How about time.deltaTime?
Your answer
![](https://koobas.hobune.stream/wayback/20220612150331im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
TouchPhase doesn't end when player swipes off screen 1 Answer
How to get intermediate swipe positions? 1 Answer
Reset touch Vector after movement 2 Answers
Taps are not detected on mobile 1 Answer