Reputation: 147
I am building a game for android in unity 5.3.5, when the scree is tapped the player moves upwards and have to keep tapping, however when the game starts the player falls down and out of play area! what i want to do is have the player stationary when game starts so when screen is tapped the game begins! here is the code..
public class Player : MonoBehaviour {
public string currentColor;
public float jumpForce = 10f;
public Rigidbody2D circle;
public SpriteRenderer sr;
public Color blue;
public Color yellow;
public Color pink;
public Color purple;
public static int score = 0;
public Text scoreText;
public GameObject obsticle;
public GameObject colorChanger;
void Start () {
setRandomColor ();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Jump") || Input.GetMouseButtonDown (0))
{
circle.velocity = Vector2.up * jumpForce;
}
scoreText.text = score.ToString ();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Scored")
{
score++;
Destroy (collision.gameObject);
Instantiate (obsticle, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag == "ColorChanger")
{
setRandomColor ();
Destroy (collision.gameObject);
Instantiate(colorChanger, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag != currentColor) {
Debug.Log ("You Died");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
score = 0;
}
if (collision.tag == "Floor")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
void setRandomColor()
{
int rand = Random.Range (0, 4);
switch (rand)
{
case 0:
currentColor = "Blue";
sr.color = blue;
break;
case 1:
currentColor = "Yellow";
sr.color = yellow;
break;
case 2:
currentColor = "Pink";
sr.color = pink;
break;
case 3:
currentColor = "Purple";
sr.color = purple;
break;
}
}
}
Upvotes: 0
Views: 161
Reputation: 1601
Make circle.isKinematic = true;
at first.
On first tap make it to circle.isKinematic = false;
Upvotes: 1