Shooting was slow and dont know whats the best way to add sound and muzzle flash so no delay occur
i am using Camera.main.ScreenToWorldPoint to get touch position and then use lookat function to Look at touch position and then fite rocket at touch position . My code is working but it little laggy , Plz anyone help m to optimize my code . or show me a way how to fire and play sound and flash simultaneously without delay . here is my code
void FixedUpdate() { int fingerCount = 0; foreach (Touch touch in Input.touches) { if (touch.phase == TouchPhase.Began || touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled && touch.phase != TouchPhase.Moved && touch.phase != TouchPhase.Stationary) fingerCount++;
if (fingerCount > 0)
{
worldPos = Camera.main.ScreenToWorldPoint(touch.position);
Vector2 touchPos = new Vector2(worldPos.x, worldPos.y);
barrelEnd.LookAt(touchPos);
// Debug.DrawRay(touch.position, touchPos, Color.red);
Rigidbody2D rocketInstance;
rocketInstance = Instantiate(SpherePrefab, worldPos, Quaternion.identity) as Rigidbody2D;
rocketInstance.velocity = (barrelEnd.forward * 7000);
audioSource.Play();
if (MuzzleFlashPrefab)
{
var flash = (GameObject)Instantiate(MuzzleFlashPrefab, barrelEnd.position, Quaternion.identity);
flash.transform.parent = transform;
}
if (touchPos.x > 0 && !facingRight)
Flip();
if (touchPos.x < 0 && facingRight)
Flip();
}
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale; // Fliping player to avoid left and righ animation .
theScale.x *= -1;
transform.localScale = theScale;
} }
Your answer
Follow this Question
Related Questions
How do I define the direction and speed of a projectile separately? (JS) 0 Answers
am trying to instantiate a 3D object to fly in the direction where my mouse is pointing 0 Answers
Detect and destroy a 2D collider if I click with mouse on it 1 Answer
How to make raycast range on this script? 0 Answers
Raycast bug is making me go nnuts,Raycast bug is making me go nuts 0 Answers