So I am trying to make a simple android game in which player has to move two objects in different halfs of the screen at the same time using swipes. It works just fine if I try to swipe to move one object and then the second one a moment after the first. But if I try swiping in two halfs at the same time it won't work. I tried managing with so-called multi-tuch but... well I need extra help.
public static event OnSwipeInput SwipeEvent;
public delegate void OnSwipeInput(Vector2 direction,bool side);
public static event OnSwipeInput SwipeEvent2;
private Vector2 tapPosition;
private Vector2 tapPosition2;
private Vector2 delta;
private Vector2 delta2;
private float deadZone = 30;
private bool isSwiping;
private bool isSwiping2;
private bool isMobile;
private bool left_side = true;
private bool left_side2 = true;
void Start()
{
isMobile = Application.isMobilePlatform;
isMobile = true;
Debug.Log("aaaa "+isMobile);
}
void Update()
{
float p;
if (!isMobile)
{
p = Mathf.Abs(Input.mousePosition.x) - Screen.width / 2;
if (Input.GetMouseButtonDown(0))
{
isSwiping = true;
tapPosition = Input.mousePosition;
if (p < 0)
left_side = true;
else
left_side = false;
}
else if(Input.GetMouseButtonDown(0))
ResetSwipe();
}
else
{
if (Input.touchCount >= 1) //try
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
p = Mathf.Abs(Input.GetTouch(0).position.x) - Screen.width / 2;
if (p < 0)
left_side = true;
else
left_side = false;
isSwiping = true;
tapPosition = Input.GetTouch(0).position;
}
else if (Input.GetTouch(0).phase == TouchPhase.Canceled || Input.GetTouch(0).phase == TouchPhase.Ended)
{
ResetSwipe();
}
}
if (Input.touchCount >= 2)
{
Input.multiTouchEnabled = true;
if (Input.GetTouch(1).phase == TouchPhase.Began)
{
p = Mathf.Abs(Input.GetTouch(1).position.x) - Screen.width / 2;
if (p < 0)
left_side2 = true;
else
left_side2 = false;
isSwiping2 = true;
tapPosition2 = Input.GetTouch(1).position;
}
else if (Input.GetTouch(1).phase == TouchPhase.Canceled || Input.GetTouch(1).phase == TouchPhase.Ended)
{
ResetSwipe2();
}
}
}
CheckSwipe();
}
private void CheckSwipe(){
if(Input.touchCount >= 1 || Input.GetMouseButton(0))
{
Debug.Log("there is 1 swipe");
delta = Vector2.zero;
if (isSwiping)
{
if (!isMobile && Input.GetMouseButton(0))
{
delta = (Vector2)Input.mousePosition - tapPosition;
}
else if (Input.touchCount == 1)
{
delta = (Vector2)Input.GetTouch(0).position - tapPosition;
}
}
if (delta.magnitude > deadZone)
{
if (SwipeEvent != null)
{
if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
{
SwipeEvent.Invoke(delta.x > 0 ? Vector2.right : Vector2.left, left_side);
}
else
{
SwipeEvent.Invoke(delta.y > 0 ? Vector2.up : Vector2.down, left_side);
}
}
ResetSwipe();
}
}
else
{
Debug.Log("there are NO swipes");
}
if (Input.touchCount == 2)
{
Debug.Log("there are 2 swipes");
delta2 = Vector2.zero;
if (isSwiping2)
{
if (Input.touchCount == 2)
{
delta2 = (Vector2)Input.GetTouch(1).position - tapPosition;
}
}
if (delta2.magnitude > deadZone)
{
if (SwipeEvent2 != null)
{
if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
{
SwipeEvent2.Invoke(delta2.x > 0 ? Vector2.right : Vector2.left, left_side2);
}
else
{
SwipeEvent2.Invoke(delta2.y > 0 ? Vector2.up : Vector2.down, left_side2);
}
}
ResetSwipe2();
}
}
}
private void ResetSwipe()
{
isSwiping=false;
tapPosition=Vector2.zero;
delta = Vector2.zero;
}
private void ResetSwipe2()
{
isSwiping2 = false;
tapPosition2 = Vector2.zero;
delta2 = Vector2.zero;
}
As you can see I am trying to operate with swipes differently seeing how many touches there are. No luck so far.
Any advice will be appriciated!
thank you
I want my game to exit and go to a separate Activity when the user collides. For example, I want it to go back to my main menu. I've tried adding a Button, new intent method etc and these don't work. I can't find the answer anywhere and i'm not knowledgeable enough to do this as I'm learning. I've trawled the internet but nothing has really helped. Any advice would be awesome. My idea was to change the newgame() method to exit the game?
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.ArrayList;
import java.util.Random;
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
private Threading threading;
private BackgroundImage bg;
public static final int WIDTH = 856;
public static final int HEIGHT = 480;
public static final int MovementSpeed = -8;
private Player player;
private ArrayList<BorderTop> borderTop;
private ArrayList<BorderBottom> borderBottom;
private ArrayList<Smokepuff> smoke;
private ArrayList<Missile> missiles;
private int MaximumBorderHeight;
private int MinimumBorderHeight;
private boolean topDown = true;
private boolean botDown = true;
private int progressDifficulty = 20;
private Random rand = new Random();
private boolean newGameCreated;
private long smokeStartTime;
private long missileStartTime;
public GamePanel(Context context) {
super(context);
getHolder().addCallback(this);
setFocusable(true);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
bg = new BackgroundImage(BitmapFactory.decodeResource(getResources(), R.drawable.grassbg1));
player = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.smallcatfinal), 40, 44, 1);
borderTop = new ArrayList<BorderTop>();
borderBottom = new ArrayList<BorderBottom>();
smoke = new ArrayList<Smokepuff>();
missiles = new ArrayList<Missile>();
smokeStartTime= System.nanoTime();
missileStartTime = System.nanoTime();
threading = new Threading(getHolder(), this);
threading.setRunning(true);
threading.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
int counter = 0;
while (retry && counter < 1000) {
counter++;
try {
threading.setRunning(false);
threading.join();
retry = false;
threading = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!player.getPlaying()) {
player.setPlaying(true);
player.setUp(true);
}else{
player.setUp(true);
}
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
player.setUp(false);
return true;
}
return super.onTouchEvent(event);
}
public void update() {
if (player.getPlaying()) {
bg.update();
player.update();
MaximumBorderHeight = 30 + player.getScore() / progressDifficulty;
if (MaximumBorderHeight > HEIGHT / 4) MaximumBorderHeight = HEIGHT / 4;
MinimumBorderHeight = 5 + player.getScore() / progressDifficulty;
//check bottom border collision
for (int i = 0; i < borderBottom.size(); i++) {
if (collision(borderBottom.get(i), player))
player.setPlaying(false);
}
//check top border collision
for (int i = 0; i < borderTop.size(); i++) {
if (collision(borderTop.get(i), player))
player.setPlaying(false);
}
this.updateTop();
this.updateBottom();
long missileElapsed = (System.nanoTime() - missileStartTime) / 1000000;
if (missileElapsed > (2000 - player.getScore() / 4)) {
//first missile always goes down the middle
if (missiles.size() == 0) {
missiles.add(new Missile(BitmapFactory.decodeResource(getResources(), R.drawable.
missile), WIDTH + 10, HEIGHT / 2, 45, 15, player.getScore(), 13));
} else {
missiles.add(new Missile(BitmapFactory.decodeResource(getResources(), R.drawable.missile),
WIDTH + 10, (int) (rand.nextDouble() * (HEIGHT - (MaximumBorderHeight * 2)) + MaximumBorderHeight), 45, 15, player.getScore(), 13));
}
//reset timer
missileStartTime = System.nanoTime();
}
//loop through every missile and check collision and remove
for (int i = 0; i < missiles.size(); i++) {
//update missile
missiles.get(i).update();
if (collision(missiles.get(i), player)) {
missiles.remove(i);
player.setPlaying(false);
break;
}
//remove missile if it is way off the screen
if (missiles.get(i).getX() < -100) {
missiles.remove(i);
break;
}
}
long elapsed = (System.nanoTime() - smokeStartTime) / 1000000;
if (elapsed > 120) {
smoke.add(new Smokepuff(player.getX(), player.getY() + 10));
smokeStartTime = System.nanoTime();
}
for (int i = 0; i < smoke.size(); i++) {
smoke.get(i).update();
if (smoke.get(i).getX() < -10) {
smoke.remove(i);
}
}
} else {
newGameCreated = false;
if (!newGameCreated) {
newGame();
}
}
public boolean collision (GameObject a, GameObject b)
{
if (Rect.intersects(a.getRectangle(), b.getRectangle())) {
return true;
}
return false;
}
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
final float ScaleX = (float) getWidth() / WIDTH*1.f;
final float ScaleY = (float) getHeight() / HEIGHT*1.f;
if (canvas != null) {
final int savedState = canvas.save();
canvas.scale(ScaleX, ScaleY);
bg.draw(canvas);
player.draw(canvas);
for(Smokepuff sp: smoke)
{
sp.draw(canvas);
}
//draw missiles
for(Missile m: missiles)
{
m.draw(canvas);
}
for(BorderTop bt: borderTop)
{
bt.draw(canvas);
}
for(BorderBottom bb: borderBottom)
{
bb.draw(canvas);
}
canvas.restoreToCount(savedState);
}
}
public void updateBottom() {
if (player.getScore() % 40 == 0) {
borderBottom.add(new BorderBottom(BitmapFactory.decodeResource(getResources(), R.drawable.brick),
borderBottom.get(borderBottom.size() - 1).getX() + 20, (int) ((rand.nextDouble()
* MaximumBorderHeight) + (HEIGHT - MaximumBorderHeight))));
}
//update bottom border
for (int i = 0; i < borderBottom.size(); i++) {
borderBottom.get(i).update();
//if border is moving off screen, remove it and add a corresponding new one
if (borderBottom.get(i).getX() < -20) {
borderBottom.remove(i);
//determine if border will be moving up or down
if (borderBottom.get(borderBottom.size() - 1).getY() <= HEIGHT - MaximumBorderHeight) {
botDown = true;
}
if (borderBottom.get(borderBottom.size() - 1).getY() >= HEIGHT - MinimumBorderHeight) {
botDown = false;
}
if (botDown) {
borderBottom.add(new BorderBottom(BitmapFactory.decodeResource(getResources(),R.drawable.brick
), borderBottom.get(borderBottom.size() - 1).getX() + 20, borderBottom.get(borderBottom.size() - 1
).getY() + 1));
} else {
borderBottom.add(new BorderBottom(BitmapFactory.decodeResource(getResources(), R.drawable.brick
), borderBottom.get(borderBottom.size() - 1).getX() + 20, borderBottom.get(borderBottom.size() - 1
).getY() - 1));
}
}
}
}
public void updateTop()
{
if (player.getScore() % 50 == 0) {
borderTop.add(new BorderTop(BitmapFactory.decodeResource(getResources(), R.drawable.brick), borderTop.get(borderTop.size() - 1).getX() + 20, 0, (int) ((rand.nextDouble() * (MaximumBorderHeight)) + 1)));
}
for (int i = 0; i < borderTop.size(); i++) {
borderTop.get(i).update();
if (borderTop.get(i).getX() < -20) {
borderTop.remove(i);
if (borderTop.get(borderTop.size() - 1).getHeight() >= MaximumBorderHeight) {
topDown = false;
}
if (borderTop.get(borderTop.size() - 1).getHeight() <= MinimumBorderHeight) {
topDown = true;
}
if (topDown) {
borderTop.add(new BorderTop(BitmapFactory.decodeResource(getResources(), R.drawable.brick), borderTop.get(borderTop.size() - 1).getX() + 20, 0, borderTop.get(borderTop.size() - 1).getHeight() + 1));
} else {
borderTop.add(new BorderTop(BitmapFactory.decodeResource(getResources(), R.drawable.brick), borderTop.get(borderTop.size() - 1).getX() + 20, 0, borderTop.get(borderTop.size() - 1).getHeight() - 1));
}
}
}
}
public void newGame() {
borderBottom.clear();
borderTop.clear();
missiles.clear();
smoke.clear();
MinimumBorderHeight = 5;
MaximumBorderHeight = 30;
player.resetDY();
player.resetScore();
player.setY(HEIGHT/2);
//create initial borders
//initial top border
for(int i = 0; i*20<WIDTH+40;i++)
{
// first top border create
if(i==0)
{borderTop.add(new BorderTop(BitmapFactory.decodeResource(getResources(),R.drawable.brick
),i*20,0, 10));
}
else
{
borderTop.add(new BorderTop(BitmapFactory.decodeResource(getResources(),R.drawable.brick
),i*20,0, borderTop.get(i-1).getHeight()+1));
}
}
//initial bottom border
for(int i = 0; i*20<WIDTH+40; i++)
{
//first border ever created
if(i==0)
{
borderBottom.add(new BorderBottom(BitmapFactory.decodeResource(getResources(),R.drawable.brick)
,i*20,HEIGHT - MinimumBorderHeight));
}
//adding borders until the initial screen is filed
else
{
borderBottom.add(new BorderBottom(BitmapFactory.decodeResource(getResources(), R.drawable.brick),
i * 20, borderBottom.get(i - 1).getY() - 1));
}
}
newGameCreated = true;
}
}
So im using this code to move around an object, but i dont know how to make it collide with the walls. No matter what I do the gameobject just goes through the walls or if it hits them they bounce. Sorry if its too simple I am new.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class controller : MonoBehaviour {
private float dist;
private bool dragging = false;
private Vector3 offset;
private Transform toDrag;
public Text score;
public int counterofbeers;
public Touch touch;
public bool lampa=false;
void Awake(){
counterofbeers = 0;
}
void Update() {
score.text= counterofbeers.ToString ();
Vector3 v3;
if (Input.touchCount != 1) {
dragging = false;
return;
}
touch = Input.touches[0];
Vector3 pos = touch.position;
if(touch.phase == TouchPhase.Began) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(pos);
if(Physics.Raycast(ray, out hit) && (hit.collider.tag == "Draggable"))
{
Debug.Log ("Here");
toDrag = hit.transform;
dist = hit.transform.position.z - Camera.main.transform.position.z;
v3 = new Vector3(pos.x,dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = toDrag.position - v3;
dragging = true;
}
}
if (dragging && touch.phase == TouchPhase.Moved) {
v3 = new Vector3(Input.mousePosition.x,dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
toDrag.position = v3 + offset;
}
if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)) {
dragging = false;
}
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag ("pickup")) {
Destroy (other.gameObject); counterofbeers++;
}
if(other.gameObject.CompareTag("wall")){
lampa=true;
}
}
}
I am working on 2D infinite runner. I have below code to take input from screen swipe to jump, slide and run fast. I am providing jumpHeight from editor and value is 500 with frame rate of 30. Code works fine generally but sometimes player jumps too high for up swipe. Similar code works as expected if input is from Keyboard. Why this is happening is beyond my understanding of unity. Any help is greatly appreciated.
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public float ForwardSpeed = 3.7f; //moves player in forward direction
public float speedOffset = 0.0f; //offset speed of player
public float JumpHeight = 250; //moves player in verticle direction
bool grounded = false; //checks if player is grounded or not
public Transform groundCheck;
float groundCheckRadius = 0.3f; //radius of groundcheck circle to check grounded bool
public LayerMask groundLayer;
Vector2 fingerStart;
Vector2 fingerEnd;
void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerStart = touch.position;
fingerEnd = touch.position;
}
if (touch.phase == TouchPhase.Moved)
{
fingerEnd = touch.position;
if (Mathf.Abs(fingerEnd.y - fingerStart.y) > 50)//Vertical swipe
{
if (fingerEnd.y - fingerStart.y > 50)//up swipe
{
Jump();
}
else if (fingerEnd.y - fingerStart.y < -50)//Down swipe
{
//Slide();
}
fingerStart = touch.position;
}
}
if (touch.phase == TouchPhase.Stationary)
{
RunFast();
}
if (touch.phase == TouchPhase.Ended)
{
fingerEnd = touch.position;
if (Mathf.Abs(fingerEnd.y - fingerStart.y) > 50)//Vertical swipe
{
if (fingerEnd.y - fingerStart.y > 50)//up swipe
{
Jump();
}
else if (fingerEnd.y - fingerStart.y < -50)//Down swipe
{
//Slide();
}
}
}
}
if (Input.GetButton("Fire1"))
{
speedOffset = 2.5f;
}
else
{
speedOffset = 0.0f;
}
if (grounded && Input.GetKeyDown(KeyCode.UpArrow))
{
grounded = false;
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpHeight));
}
//check if circle overlaps with ground layer
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
//Debug.Log(grounded);
}
void FixedUpdate()
{
//set players forward velocityto forward speed variable
Vector2 PlayerForwardvelocity = GetComponent<Rigidbody2D>().velocity;
// Vector2 PlayerJumpHeight = GetComponent<Rigidbody2D>().AddForce()
PlayerForwardvelocity.x = ForwardSpeed + speedOffset;
GetComponent<Rigidbody2D>().velocity = PlayerForwardvelocity;
}
void Jump()
{
if (grounded)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpHeight));
speedOffset = 0.0f;
}
}
void RunFast()
{
if (Input.GetButton("Fire1"))
{
speedOffset = 2.5f;
}
else
{
speedOffset = 0.0f;
}
}
}
You have 2 problems with your code.
Your first problem lies in this line of code:
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
This line of code is failing. grounded is always true. Because of this, jump is called too many times while player is not grounded.
Replace this line of code with
Collider2D playerCollider = gameObject.GetComponent<Collider2D>();
grounded = Physics2D.OverlapCircle(playerCollider.transform.position, 1, groundLayer);
OR
Collider2D playerCollider = gameObject.GetComponent<Collider2D>();
grounded = playerCollider.IsTouchingLayers(groundLayer.value);
Another problem with your code is a false report.Sometimes, collider overlapping returns true even if it is false. I tried moving that part of code to LateUpdate function but that didn't fix it.
You can fix it by implementing a timer. The timer resets to 0 and starts counting to 0.5 whenever player jumps. Don't jump when timer has not reached the value it is counting to. .5 to 1 is a perfect value for this. Increment the timer with Time.deltaTime. Below is your whole code with timer and fixes.
public class PlayerControl : MonoBehaviour
{
public float ForwardSpeed = 3.7f; //moves player in forward direction
public float speedOffset = 0.0f; //offset speed of player
public float JumpHeight = 250; //moves player in verticle direction
bool grounded = false; //checks if player is grounded or not
public Transform groundCheck;
float groundCheckRadius = 0.3f; //radius of groundcheck circle to check grounded bool
public LayerMask groundLayer;
Vector2 fingerStart;
Vector2 fingerEnd;
public float resetTimer = 0.5f; //.5 second
float timerCounter = 0;
Collider2D playerCollider = null;
Rigidbody2D playerRigidBody;
void Start()
{
playerRigidBody = GetComponent<Rigidbody2D>();
playerCollider = gameObject.GetComponent<Collider2D>();
}
void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerStart = touch.position;
fingerEnd = touch.position;
}
if (touch.phase == TouchPhase.Moved)
{
fingerEnd = touch.position;
if (Mathf.Abs(fingerEnd.y - fingerStart.y) > 50)//Vertical swipe
{
if (fingerEnd.y - fingerStart.y > 50)//up swipe
{
Jump();
}
else if (fingerEnd.y - fingerStart.y < -50)//Down swipe
{
//Slide();
}
fingerStart = touch.position;
}
}
if (touch.phase == TouchPhase.Stationary)
{
RunFast();
}
if (touch.phase == TouchPhase.Ended)
{
fingerEnd = touch.position;
if (Mathf.Abs(fingerEnd.y - fingerStart.y) > 50)//Vertical swipe
{
if (fingerEnd.y - fingerStart.y > 50)//up swipe
{
Jump();
}
else if (fingerEnd.y - fingerStart.y < -50)//Down swipe
{
//Slide();
}
}
}
}
if (Input.GetButton("Fire1"))
{
speedOffset = 2.5f;
}
else
{
speedOffset = 0.0f;
}
if (grounded && Input.GetKeyDown(KeyCode.UpArrow))
{
grounded = false;
playerRigidBody.AddForce(new Vector2(0, JumpHeight));
}
//check if circle overlaps with ground layer
grounded = Physics2D.OverlapCircle(playerCollider.transform.position, 1, groundLayer);
//OR Use grounded = playerCollider.IsTouchingLayers(groundLayer.value);
//Increment Timer if it is still less than resetTimer
if (timerCounter < resetTimer)
{
timerCounter += Time.deltaTime;
}
}
void FixedUpdate()
{
//set players forward velocityto forward speed variable
Vector2 PlayerForwardvelocity = playerRigidBody.velocity;
// Vector2 PlayerJumpHeight = playerRigidBody.AddForce()
PlayerForwardvelocity.x = ForwardSpeed + speedOffset;
playerRigidBody.velocity = PlayerForwardvelocity;
}
void Jump()
{
if (grounded)
{
//Exit if timer has not reached the required value to jump again
if (timerCounter < resetTimer)
{
Debug.Log("Failed To Jump because timer has not yet reached");
return; //Exit
}
timerCounter = 0; //Reset Timer
playerRigidBody.AddForce(new Vector2(0, JumpHeight));
speedOffset = 0.0f;
Debug.Log("Jumped");
}
else
{
Debug.Log("Not on the Ground");
}
}
void RunFast()
{
if (Input.GetButton("Fire1"))
{
speedOffset = 2.5f;
}
else
{
speedOffset = 0.0f;
}
}
}
I made a Flappy Bird-like game in Unity 2D for Android. I's working & running on my device, but there is one problem with the contsols:If i tap the screen the bird flaps and flyes, but if I hold the screen the bird is keep going up and up.. I want to only sense one tap.
There is my code:
if (Input.touchCount == 1) {
didFlap = true;
}
Full code:
using UnityEngine;
using System.Collections;
public class BirdMovement : MonoBehaviour {
Vector3 velocity = Vector3.zero;
public float flapSpeed = 100f;
public float forwardSpeed = 1f;
bool didFlap = false;
Animator animator;
public bool dead = false;
float deathCooldown;
public bool godMode = false;
// Use this for initialization
void Start () {
animator = transform.GetComponentInChildren<Animator>();
if(animator == null) {
Debug.LogError("Didn't find animator!");
}
}
// Do Graphic & Input updates here
void Update() {
if(dead) {
deathCooldown -= Time.deltaTime;
if(deathCooldown <= 0) {
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
Application.LoadLevel( Application.loadedLevel );
}
}
}
else {
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
didFlap = true;
}
if (Input.touchCount == 1) {
didFlap = true;
}
}
}
// Do physics engine updates here
void FixedUpdate () {
if(dead)
return;
rigidbody2D.AddForce( Vector2.right * forwardSpeed );
if(didFlap) {
rigidbody2D.AddForce( Vector2.up * flapSpeed );
animator.SetTrigger("DoFlap");
didFlap = false;
}
if(rigidbody2D.velocity.y > 0) {
transform.rotation = Quaternion.Euler(0, 0, 0);
}
else {
float angle = Mathf.Lerp (0, -90, (-rigidbody2D.velocity.y / 3f) );
transform.rotation = Quaternion.Euler(0, 0, angle);
}
}
void OnCollisionEnter2D(Collision2D collision) {
if(godMode)
return;
animator.SetTrigger("Death");
dead = true;
deathCooldown = 0.5f;
}
}
If you wanna do it with touchcount I think you should do something like this create bool variable in start cangoup=true
Ask
if (input.touchcount==0)
Cangoup=true;
And the didflap statement should be something like this
If(cangoup)
{//everything you wrote
Cangoup=false;}
I wrote this from my iphone so there may be few mistakes but i am sure you can fix them and i think this will do you good if it doesn't just comment on the answer and ill reply