- Home /
commands.close.hint.6
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?
Answer by djmorrsee · Aug 18, 2011 at 01:34 AM
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....
thanks for the advice, for the first part of your response:
this is gonna go on the player "L_U D" stands for Left Stick Up Down so when the stick moves up or down check RN (Random Number) if its less than 7 and greater than 4. if so then "Battle" is printed in the log.
as for your second part. its not moving the character to a new map its loading a completely new setting temporarily for the duration of the battle. loading a different character mesh and everything. much like Final Fantasy VII if you have ever played that. but in the battle everything is gonna be free roam like a CoD online game.
So for the L_U D part, you would want if(Input.GetAxis("L_U D") != 0). I'm unsure why you were getting the error you had commented on mgear's answer. Your sure there were no typo's?
Right I think i understand your idea. You'll be changing scenes correct? DontDestroyOnLoad will ensure that whatever object you pass as its argument will show up in your new scene. So you can store the location variable in a script, keep it through the battle, then when you go back, make sure it doesnt destroy again, then use that variable to set up your position. Sorry if im not explaining very well here, or just misunderstanding you completely.
As a side note, Update is called every frame. So even on slower machines, you would average at the very least 20-30 frames per second. With the way you have the Random number working now, based on statistics, you would get a battle every 5 frames or so. So 4 battles per second, at the least. Just a small observation I made, that might be on purpose for testing reasons.
Answer by mgear · Aug 18, 2011 at 01:19 AM
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'
Follow this Question
Related Questions
Converting Variables 3 Answers
Compile Errors for Script 1 Answer
Login String to Bool Errors 1 Answer
Mathf.Lerp float between 0 and 1 based on boolean input 3 Answers
what the hell is a int? (or a float, or boolean, or string!) 3 Answers