Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by DubstepDragon · Jan 31, 2013 at 10:00 PM · characterchangecontrolstatemagic

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
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MD_Reptile · Feb 01, 2013 at 12:51 AM 0
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
         }
       }
     }
   } 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DubstepDragon · Feb 01, 2013 at 11:01 AM 0
Share

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.

avatar image DubstepDragon · Feb 01, 2013 at 11:56 AM 0
Share

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?

avatar image DubstepDragon · Feb 01, 2013 at 02:52 PM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

change player skin in real time? 1 Answer

Character Controller - Collision Detection 1 Answer

Using W,A,S & D to control character. 1 Answer

animation problem (transform.position) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges