Input.GetTouch don't work

Hi
I keep working on the same code. I want to get an object in the place you touch the display. I have this code:

public Rigidbody bullet;
public float power = 1500f;
public float moveSpeed = 2f;


void Update () {
	
	float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
	float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
	transform.Translate(h, v, 0);

	        int i = 0;
    while (i < Input.touchCount) {
        if (Input.GetTouch(i).phase == TouchPhase.Began)
          Rigidbody clone = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
        
        ++i;
    }
	/*
	if(Input.GetButtonUp("Fire1"))
	{
		
		Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation)as Rigidbody;
		Vector3 fwd = transform.TransformDirection(Vector3.forward);
		instance.AddForce(fwd * power);
		
	}*/
}

}
But I get:

Assets/Shooter.cs(20,106): error CS1023: An embedded statement may not be a declaration or labeled statement

Here it’s 23 line.

When I added {} in the loop if it freezes my phone. When I tested for the application "Remote" is also suspended unity.

2 Answers

2

Well, I write script for touch events:

 public Rigidbody bullet; //check reference in Inspector
 public float power = 1500f;
 public float moveSpeed = 2f;

 void Update () {
  float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
  float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
  transform.Translate(h, v, 0);

  Rigidbody myClone = null;
  //For editor
  #if UNITY_EDITOR
  if(Input.GetMouseButtonDown(0)) {
   Vector3 pos = Input.mousePosition; //get screen position of mouse
   //Calculate screen position of touch in position in world, "z" component is  more 0, example 0.3f - default near clip plane for perspective camera 
   Vector3 p = Camera.main.ScreenToWorldPoint(new Vector3(pos.x, pos.y, Camera.main.nearClipPlane));
   //Set new position "p"
   myClone = Instantiate(bullet, p, transform.rotation) as Rigidbody;
   myClone.AddForce(power*Camera.main.transform.forward);
  }
  #endif

  //For touch
  #if UNITY_ANDROID || UNITY_IPHONE
  if (Input.touchCount > 0) {
   for(int i = 0; i < Input.touchCount; i++) {
    if (Input.GetTouch(i).phase  == TouchPhase.Began) {
     Vector3 pos = Input.GetTouch(i).position; //get screen position of touch
     //Calculate screen position of touch in position in world, "z" component is  more 0, example 0.3f - default near clip plane for perspective camera 
     Vector3 p = Camera.main.ScreenToWorldPoint(new Vector3(pos.x, pos.y, Camera.main.nearClipPlane));
     //Set new position "p"
     myClone = Instantiate(bullet, p, transform.rotation) as Rigidbody;
     myClone.AddForce(power*Camera.main.transform.forward);
    }
   }
  }
  #endif
 }

I hope that it will help you.

Your code work but I need something other. I want the created object where I touch the display, not only front of the camera. I always added thi code, maybe on this code is something wrong: Vector3 fwd = transform.TransformDirection(Vector3.forward); instance.AddForce(fwd * power);

@woks On how many I understood, the script is attached to the camera. When you touch the screen, from that place the bullet with a certain characteristic shall take off. If so, I a little add a script in the answer.

Yes, the script is added to the camera. I still shoot in the center of the camera ... Maybe I can do it simply, for example. that in the code: Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation)replace transform.position kind of onTouch.position?

@woks touch coordinate is coordinate on screen, if you want shoot from center, than you don't calculate position on screen. Use position camera. I update my answer.

@zharik86 Your script works in a way that shoots from center but I want him fired from the position where the touch display.

you are adding your i integer the first time you enter your touch count by one so next time this statement
i< Input.touchCount is false and nothing will happen you should
check

 if (Input.touchCount > 0){
            //doSomething
}