- Home /
Changing the state of the character via a trigger
Hi all, I am working on a project that involves the character changing his state when stood on a trigger. There are four states:
None/Neutral
Air
Fire
Water
Upon standing on a trigger with the specific tag, the player will shift into a different state, and on pressing the left-mouse button, will instantiate a different particle/rigidbody.
I am, however, not very successful, and I need immediate assistance, as I am not as good in programming as the majority in the forums. This script is in C#.
using UnityEngine;
using System.Collections;
public class State : MonoBehaviour {
public GameObject particleNone;
public GameObject particleAir;
public GameObject particleFire;
public GameObject particleWater;
private int setStateNone;
private int setStateAir;
private int setStateFire;
private int setStateWater;
void OnTriggerEnter(Collider other) {
if(other.gameObject.tag == "NonePower") {
setStateNone();
}
if(other.gameObject.tag == "AirPower") {
setStateAir();
}
if(other.gameObject.tag == "FirePower") {
setStateFire();
}
if(other.gameObject.tag == "WaterPower") {
setStateWater();
}
}
public void Main() {
state typeofstate = state.None;
if(setStateNone) {
if(Input.GetMouseButton(0)) {
Instantiate(particleNone);
}
if(particleNone == null) {
Debug.Log("THERE IS NO PARTICLE FOR *NONE*");
}
}
if(setStateAir) {
if(Input.GetMouseButton(0)) {
Instantiate(particleAir);
}
if(particleAir == null) {
Debug.Log("THERE IS NO PARTICLE FOR *AIR*");
}
}
if(setStateFire) {
if(Input.GetMouseButton(0)) {
Instantiate(particleFire);
}
if(particleFire == null) {
Debug.Log("THERE IS NO PARTICLE FOR *FIRE*");
}
}
if(setStateWater) {
if(Input.GetMouseButton(0)) {
Instantiate(particleWater);
}
if(particleWater == null) {
Debug.Log("THERE IS NO PARTICLE FOR *WATER*");
}
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public enum state {
None,
Fire,
Water,
Air
}
}
@dubstepdragon I think your going to have to include more details to your problem. Which part of this script seems to be causing you problems? Just reading briefly over your code it seems ok.
Answer by aldonaletto · Feb 01, 2013 at 01:18 AM
You should place the code in Update instead of Main - Main isn't defined in Unity scripts, and will do nothing unless you call it explicitly. Use GetMouseButtonDown instead of GetMouseButton, or a new particle will be instantiated every frame while the button is pressed.
using UnityEngine;
using System.Collections;
public class State : MonoBehaviour {
public GameObject particleNone;
public GameObject particleAir;
public GameObject particleFire;
public GameObject particleWater;
public enum state {
None,
Fire,
Water,
Air
}
private state curState = state.None; // assume None initially
void OnTriggerEnter(Collider other) {
switch (other.tag){
case "AirPower":
curState = state.Air;
break;
case "FirePower":
curState = state.Fire;
break;
case "WaterPower":
curState = state.Water;
break;
case "NonePower":
curState = state.None;
break;
}
}
void Update() {
// use GetMouseButtonDown to avoid instantiating tons
// of particles while the mouse button is pressed:
if(Input.GetMouseButtonDown(0)){
// calculate position and rotation to instantiate particles
// this code just do it at the object's position/rotation
Vector3 pos = transform.position;
Quaternion rot = transform.rotation;
switch (curState)
case state.Air:
if (particleAir){
Instantiate(particleAir, pos, rot);
} else {
Debug.Log("THERE IS NO PARTICLE FOR *AIR*");
}
break;
case state.Fire:
if (particleFire){
Instantiate(particleFire, pos, rot);
} else {
Debug.Log("THERE IS NO PARTICLE FOR *FIRE*");
}
break;
case state.Water:
if (particleWater){
Instantiate(particleWater, pos, rot);
} else {
Debug.Log("THERE IS NO PARTICLE FOR *WATER*");
}
break;
case state.None:
if (particleNone){
Instantiate(particleNone, pos, rot);
} else {
Debug.Log("THERE IS NO PARTICLE FOR *NONE*");
}
break;
}
}
}
}
Thank you very much! Your code seems way cleaner than $$anonymous$$e - I have started program$$anonymous$$g recently. However, I am getting one error now, in the case state.Air:, case state.Fire:, case state.Water: and case state.None: lines, it states "Parser Error: Unexpected symbol 'case'". If you can point me to the right way, that would be greatly appreciated.
Sorry, just figured out there was a curly brace missing - Yep, thanks! It works like a charm now! Only one issue now - When I press the L$$anonymous$$B, the particle is instantiated but shot off at a random direction everytime I press the L$$anonymous$$B - Is there anything I can do to stop that?
Never $$anonymous$$d, fixed it - there was a random rotation script attached to the particle, because I purchased the pack. Thank you for your help - hope I did not bother you!