- Home /
Player respawning after geting two touches
I though my script was going to actually work but it's not... (newbie for coding c#)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ReSpawn : MonoBehaviour { private Vector3 spawn;
// Start is called before the first frame update
void Start()
{
spawn = transform.position;
}
void Update()
{
if (transform.position.y < -2)
{
Die();
}
}
void Die()
{ if (Input.touchCount > 0) { Touch touch = Input.GetTouch(1); transform.position = spawn; }
}
}
@Bunny83 @Eric5h5 @robertbu @aldonaletto @tanoshimi @duck @whydoidoit @fafase @clunk47 @Statement @Kiwasi @Sebastian 2 @AT-Brackeys
You should clarify a little bit what you want to achieve, and how you want your code to work. I'm not much into this touching thing, but I assume your code works like this: 1. At the start you set the position of the object that has the ReSpawn script attached. 2. In Update you check every frame, if the mentioned object is lover on the y axis then -2. 3. If it is, then the Die() method runs, and checks if there is at least one touch happened. If the answer is true, then you save the second Touch of a double tap (?$$anonymous$$aybe I'm wrong, as I said, I'm not much into this stuff), I think for later use. And in the end you set it's position to the spawn point.
Check if you attached this script to the GameObject you want to move (I assume this is the player). If you want to respawn the player after it gets two touches, then simply make an int variable, set it to 0 at the start, then increase it if there is touch in update, and then check if it is higher than 1. Do not forget to reset it at respawn.
public class ReSpawn : $$anonymous$$onoBehaviour
{
private Vector3 spawn; //I don' know if you set it's value somewhere. You should.
private int touches;
void Start()
{
spawn = transform.position;
touches = 0;
}
void Update()
{
if (Input.touchCount > 0)
{
touches++;
if(touches >= 2)
Die();
}
}
void Die()
{
if (Input.touchCount > 0)
{
transform.position = spawn;
touches = 0;
}
}
}
Your answer
Follow this Question
Related Questions
Moving an object forward, killing the object and respawning a new one. 1 Answer
multiplayer die and respawn 0 Answers
ScrollRect and Buttons do not communicate. 1 Answer
touch of the controls 0 Answers