Checking if index of Vector3 array matches integer with if statement?
What I have is a script that holds a public array of Vector3 coordinates. I have an integer, currently set to 1, which, on Death(), once it goes through a foreach loop and finds the index that matches the integer, should break;, and then set the player position (transform.position, since this script is attached to the player) to the position of that Vector3. It doesn't quite work as it is right now. It tells me it can't convert a float to a bool (CS0030), but I can't figure out what the float or bool would be.
This is my respawn mechanic. The idea is that, once I do something in the game, I'll unlock a checkpoint (and save that to disk, once I get to that part), by incrementing the integer by 1. I'd also prefer to use transforms, as it would allow me to just place cubes wherever I want my checkpoints, or even include that in the prefab, which would make things so much easier.
This is the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Player : MonoBehaviour {
[SerializeField]
private Rigidbody playerRigid;
[SerializeField]
private float playerSpeed;
[SerializeField]
private float playerJumpSpeed;
private bool isGrounded;
public Vector3[] checkpoints;
[SerializeField]
private int unlockedCheckpoint = 1;
private void Start(){
isGrounded = true;
}
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
playerRigid.AddForce(Vector3.up * playerJumpSpeed);
}
isGrounded = false;
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
playerRigid.AddForce (movement * playerSpeed);
}
void Update() {
if (transform.position.y < -10.0f) {
Death ();
}
}
void OnCollisionStay(){
isGrounded = true;
}
void Death(){
print ("ded");
foreach (int checkpoint in checkpoints) {
if (checkpoint == unlockedCheckpoint) {
print ("found the right one!" + checkpoint);
break;
}
}
}
}
Your answer