help with a random battle generator?

i am making a gale that is gonna consist of a battle system unlike any other. but i have to start at the beginning to even start making it into what i want it, and the beginning is a little more complicated than i originally intended.

i started my scripting already on making the random battles happen, but for my first problem i keep getting this error:

Assets/FPS RPG_Scripts/RandomEncounterScript.cs(21,17): error CS0029: Cannot implicitly convert type ‘float’ to ‘bool’

with this code:

using UnityEngine;

using System.Collections;

public class RandomEncounterScript : MonoBehaviour {

private int RN;
public bool randEncounter;
public bool inBattle;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

	RN = Random.Range(1, 10);
	
	if (Input.GetAxis("L_U D")){
	if(RN > 4){	
			if(RN < 7){
		Debug.Log ("battle");
			}
		}
	}
}

}

and my second problem. this is going to load another level called battle or something and after the battle is finished i want to have my character resume from the exact same spot as before the random battle occured. how can i cache or store the player coordinates on the map that it was on before the battle?

2 Answers

2

Havent used “Input.GetAxis” before, but it returns Float value, not boolean.

So, should be then:

if (Input.GetAxis("L_U D")!=0){

now it says Operator !=' cannot be applied to operands of type string' and `int'

As for your first question, Input.GetAxis returns always returns a float value (and always exists) between -1 and 1. As I’m not sure what your intentions were with that line, its hard to suggest a workaround. You would need to do something like

if(Input.GetAxis("L_U D") > 0){  //Moving to left (or right?)
or
if(Input.GetAxis("L_U D") != 0){ //axis is tilted

Again, its hard to say without knowing what you were trying to do. For your second question, look into Object.DontDestroyOnLoad(). Saving an empty object with a script that gets your player.transform.position when the scene changes, then applies it when the scene comes back should work. Personally, I’ve never worked with DontDestroyOnLoad, but it should work.

EDIT: I was ninja’d…