• 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
Question by HailTheGreatWolf · May 29, 2013 at 09:49 AM · aidistanceloadlevel

Adding a load level to this script.

var currentCell : GameObject; var playerMovementScript : PlayerMovementScript; var playerCell : GameObject; var goalDoor : GameObject; var shortestPathSoFar : float; @HideInInspector var waitToStart : int = 5; var currentMoveSpeed : float = 2;

function Awake ()

{ shortestPathSoFar = Mathf.Infinity; playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript); waitToStart = 5; }

function OnTriggerEnter (hitTrigger : Collider)

{ if (hitTrigger.tag == "AIpathCell") currentCell = hitTrigger.gameObject; }

function OnTriggerExit (other : Collider){

 if (other.tag == "AIpathCell"){

     Statics.enemySpacesMove += 1;
 }

}

function Update ()

{

if (Statics.enemyControlOn){

if(PlayerMovementScript.CrntDroneName == "TF_Sleeper1") {

 if (Statics.enemySpacesMove < 2)
 {
 
 if (waitToStart <= 0)
 {
     playerCell = playerMovementScript.currentCell;
     for (var doorCheckingNow : GameObject in currentCell.GetComponent(AIpathCellScript).doors)
     {
         for (var i : int = 0; i <= doorCheckingNow.GetComponent(AIpathDoorScript).cells.length - 1; i++)
             {
                 if (doorCheckingNow.GetComponent(AIpathDoorScript).cells[i] == playerCell)

                             if (doorCheckingNow.GetComponent(AIpathDoorScript).doorsToCells[i] < shortestPathSoFar)
                 {
                     goalDoor = doorCheckingNow;
                     shortestPathSoFar = doorCheckingNow.GetComponent(AIpathDoorScript).doorsToCells[i];
                 }
             }
         }
         shortestPathSoFar = Mathf.Infinity;
     }
     waitToStart -= 1;
 
 if(goalDoor && currentCell != playerCell)
     transform.position += (goalDoor.transform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
         animation.Play("sleepWalk");

 }
 if (Statics.enemySpacesMove > 1){
 
 animation.Stop("sleepWalk");
 //Turn_Master.currentEnemy += 1;
 if(PlayerMovementScript.crntDrone == PlayerMovementScript.arrayLength-1){
         Statics.currentTurn = "plyer";
         PlayerMovementScript.crntDrone = 0;
 }
 else{PlayerMovementScript.crntDrone +=1;}
 Statics.enemySpacesMove = 0;
 }
 }

} }

Basically all I need is to add in a load application when this object gets close enough to the player, but where do I add in the distance variable and stuff? Do I need an else in there somewhere?

Comment
Graham-Dunnett

People who like this

-1 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 Graham-Dunnett ♦♦ · May 29, 2013 at 09:50 AM 1
Share

You might want to format the code correctly if expect someone to help.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by DubstepDragon · May 29, 2013 at 10:17 AM

First of all, we all are sick and tired of people not formatting their code correctly (no offense meant), and so it is a pain in the ass to attempt and read it. However, it is dead easy to load another level/scene via a trigger. So, follow these steps:

  • Add a collider to the object you want to load the level on collision from

  • Attach this simple script to it:

    void OnTriggerEnter(Collider other) {

       Application.LoadLevel("Your level's name or number");
         
     }
    
    
    
    
  • After that, make sure the object's collider component has the "Is Trigger" switched on. (Note that this will disable collision, so you might want to add another collider for collision purposes.)

I hope that helps :) Tell me if you need any further assistance.

John

Comment
Ashkan_gc

People who like this

1 Show 2 · 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 · Jun 04, 2013 at 07:19 AM 0
Share

At least mark my answer correct :/

avatar image Ashkan_gc · Jun 04, 2013 at 08:52 AM 0
Share

The only missing point in the otherwise great answer is that either the object with the trigger or the character comming to it should have a kinematic rigidbody (i.e. a rigidbody component with isKinematic set to true) for it to work. Also he might want to do a check in OnTriggerEnter to see who entered this zone. Maybe checking player tag or name ...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

SphereCastAll sees for miles 2 Answers

Make Navmesh agent follow closest object with tag 0 Answers

Too Close To The Trap and melee attacking enemys. 1 Answer

Help Calculating Shortest Total Distance Travelled By a Combination of Objects 0 Answers

Adding Distance in javascript 2 Answers


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