Hi, i want to touch the screen and instanciate a bullet in that position
I have this code for instantiate the bullet:
> public GameObject bullet;
public Camera myCam;
private Touch touch;
public int numeroZ = 4;
void Update()
{
for (var i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
Vector3 touchPos = Input.GetTouch(i).position;
print(touchPos);
touchPos.z = numeroZ;
var createPos = myCam.ScreenToWorldPoint(touchPos);
Instantiate(bullet, createPos + new Vector3(0, 0, -10), Quaternion.identity);
}
}
}
And this code for the movement of the bullet:
public Vector3 mov;
public int lifeTime;
private void Start()
{
Invoke("DestroyBullet", lifeTime);
}
void Update()
{
transform.Translate(mov * Time.deltaTime);
}
void DestroyBullet()
{
Destroy(gameObject);
}
And this is the result, even if I touch the edges of the screen and the bullet instantiate in the the touch position when it moves doesn’t go in the touch position direction