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
0
Question by Sindario · May 05, 2014 at 11:25 PM · c#pcnpc

Friendly NPC to Enemy (C#)

Okey so I need My NPC that is friendly and you can make conversation with it, to after the last conversation outcome become enemy to PC and start shooting. I need this in C# Would appreciate all the help. Here is my NPC Talk conversation Which work well. And I need to after clicking the second option and second outcome to become unfriendly to PC and starts shooting at him. I got a prefab made also with NPC_Chaser maybe it could be done somehow to replace one NPC with another I don't know, please help. Thank you.

using UnityEngine; using System.Collections;

public class DD_NPC_talk : MonoBehaviour {

 //-------------------------------------------------------------------------
 // ----- Declare Variables 
     
 // Game Objects
 private GameObject go_PC;
 
 //Dialogue Variables
 public string st_NPC_name = "Tower Guard";
 public string st_NPC_inital_text = "You there, Halt where do you think you are going?";
 public string st_PC_response_1 = "Sorry Sir, I was looking for the guards base,\nI want to report a crime";
 public string st_PC_response_2 = "Shove off you trout faced old Fart!!";
 public string st_NPC_outcome1 = "You poor young scamp, it is in the north west part of town, but watch out for the Pirates";
 public string st_NPC_outcome2 = "Well I have never been so insulted, I hope the guards fill you with holes, and the try and fill them with their pork swords!";
 public string st_NPC_return_text = "Leave me alone before I call the guards!";
 
 private string st_NPC_outcome;
 private bool bl_dialogue_on;
 private bool bl_PC_responded;
 private bool bl_conversation_complete;
 
 
 private DD_Level_Manager LM_Script;
 
 //-------------------------------------------------------------------------
 // Start - only called once 
 void Start()
 {
     
     // Find the level manager script
     LM_Script = GameObject.Find("Level_Manager").GetComponent<DD_Level_Manager>();
     
     // Find the PC GameObject
     go_PC = GameObject.Find("PC");        
 }
 
 //-------------------------------------------------------------------------
 // Update is called once per frame
 void Update () 
 {
     // Is the PC in the Scene
     if(go_PC)
     {
         //  when the PC is close Turn the GUI box and start Converstation
         if (Vector3.Distance( transform.position , go_PC.transform.position) < 3 )
         {    
             bl_dialogue_on = true;                        
         }
         else
         {
             bl_dialogue_on = false;    
         }
     }
     else
     {
         go_PC = GameObject.Find("PC");    
     }
     
 
 }//---
 
 // ----- Interface Control GUI Display -----------------------------------------
 void OnGUI()
 {
     if (bl_dialogue_on)
     {        
         // Draw Outline Box
         GUI.Box(new Rect ((Screen.width/2)-200, (Screen.height/2)-150, 400, 250), st_NPC_name);
         
         if (!bl_conversation_complete)
         {                
             if (!bl_PC_responded)
             {                    
                 // NPC Inital text
                 GUI.Label(new Rect ( (Screen.width/2)-160, (Screen.height/2)-120, 350, 50), st_NPC_inital_text);
                 
                 Screen.lockCursor = false;
                                   
                 // PC option 1
                 if ( GUI.Button(new Rect ( (Screen.width/2)-180, (Screen.height/2)-60, 350, 60), st_PC_response_1)  )    
                 {
                        // Set NPC response 1 and flag we have spoken    
                     st_NPC_outcome = st_NPC_outcome1;
                     bl_PC_responded = true;    
                     
                  }
                 
                 // PC option 2                
                 if ( GUI.Button(new  Rect ( (Screen.width/2)-180, (Screen.height/2)+10, 350, 60), st_PC_response_2)  )    
                 {
                     // Set NPC response 1 and flag we have spoken    
                     st_NPC_outcome = st_NPC_outcome2;
                     bl_PC_responded = true;              
                 }    
                }
             else 
             {
                 // bl_PC_responded is now true
                 
                 // Display the NPC response to the PC's Choice
                 GUI.Label(new Rect ( (Screen.width/2)-180, (Screen.height/2)-130, 350, 50), st_NPC_outcome);
                         
                 // PC Goodbye - and set end flag
                 if ( GUI.Button(new Rect ( (Screen.width/2)-180, (Screen.height/2)-60, 350, 60), " Thanks, Goodbye")  )    
                 {        
                      bl_conversation_complete = true    ;
                     Screen.lockCursor = true;
                 }
             }    
         }
         else
         {
             // bl_conversation_complete is now true
             
             // Display text indicating we have spoken
             GUI.Label(new Rect ( (Screen.width/2)-180, (Screen.height/2)-60, 350, 50), st_NPC_return_text);    
         }
     }    
     
 }// ---

}// -----

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · May 05, 2014 at 11:32 PM

Yes you could replace one with the other.

You would use the Instantiate command to create the new one. You would use the Destroy command to get rid of the old one (DestroyImmediate might be necessary for it to look clean)

Another approach would be to simply have another component that is your attack behavior. You disable the talk component and enable the attack one.

Comment
Add comment · 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

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Killable NPC who can kill player 1 Answer

Distribute terrain in zones 3 Answers

Refin' another script in relation to speed, and not tacking from using the Update? 1 Answer

[C#]Angle of shooting equale to mouse direction 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