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 Oobi23 · May 21, 2013 at 01:29 AM · raycastendgame

Raycast/Collider End Level

Hi there, So I am trying to use OnTriggerEnter Collider to end a game. Note that I am using Raycast for all my movements. So most of the end level methods do not work.I know I have to use something to do with Application.LoadLevel, but I am not sure how to write up this code. So how do I write up the code to end a game with raycasting on a tagged object?. This is my movement code so far.

 private var insideObject:Transform;
 
 private var okToMove:boolean;
 private var showingMission:boolean;
 function Start(){
     insideObject=transform.Find("Capsule");
     okToMove=true;
     showingMission=false;
 }
 function Update () {
     if(okToMove){
     //Debug.Log(insideObject.rotation.eulerAngles);
 var dwn = transform.TransformDirection (Vector3.down);
 var fwd = transform.TransformDirection (Vector3.forward);
 var bk = transform.TransformDirection (Vector3.back);
 var r = transform.TransformDirection (Vector3.right);
 var l = transform.TransformDirection (Vector3.left);
 var forwardPos:Vector3=transform.position + fwd; 
 var backwardPos:Vector3=transform.position + bk;
 var rightPos:Vector3=transform.position + r;
 var leftPos:Vector3=transform.position + l;
 
 var hit:RaycastHit;
 
 Debug.DrawRay(forwardPos,dwn,Color.green,5.0); 
 if (Input.GetKeyDown(KeyCode.UpArrow)) {
 
 
  if (Physics.Raycast (transform.position, fwd, hit,1)) {
        print ("There is something in front of the object!");
             if(hit.collider.gameObject.tag!="Push"){
     transform.position += Vector3.up;
     
             }
             else{
                    print ("Push!");
                    
                   hit.collider.gameObject.transform.position += Vector3.forward;
                    transform.position += Vector3.forward;
                 
             }
             
     }else{
     Debug.Log("nothing in front");
     
 
             
             
             
             
     if (Physics.Raycast (forwardPos,dwn, 11)) {
     transform.position += Vector3.forward;
                 //did we move to an empty space ?
     if (!Physics.Raycast (transform.position,dwn, .5)) {
     okToMove=false;
     InvokeRepeating("dropDown",1.0,1.0);
     
                     
 }
                 
 }
             
 }
 insideObject.rotation.eulerAngles.y=0;
 }
 
 
 
 if (Input.GetKeyDown(KeyCode.DownArrow)) {
 
  if (Physics.Raycast (transform.position, bk, hit,1)) {
        print ("There is something in front of the object!");
             if(hit.collider.gameObject.tag!="Push"){
     transform.position += Vector3.up;
             }else{
                    print ("Push!");
                   hit.collider.gameObject.transform.position += Vector3.back;
                    transform.position += Vector3.back;
                 
             }
             
     }else{
     Debug.Log("nothing in front");
             
             
     if (Physics.Raycast (backwardPos,dwn, 6)) {
     transform.position += Vector3.back;
                 //did we move to an empty space ?
     if (!Physics.Raycast (transform.position,dwn, .5)) {
     okToMove=false;
     InvokeRepeating("dropDown",1.0,1.0);
                     
 }
                 
 }
             
 }
     
 insideObject.rotation.eulerAngles.y=180;
 }
 if (Input.GetKeyDown(KeyCode.RightArrow)) {
 
 if (Physics.Raycast (transform.position, r, hit,1)) {
        print ("There is something in front of the object!");
             if(hit.collider.gameObject.tag!="Push"){
     transform.position += Vector3.up;
             }else{
                    print ("Push!");
                   hit.collider.gameObject.transform.position += Vector3.right;
                    transform.position += Vector3.right;
                 
             }
             
     }else{
     Debug.Log("nothing in front");
             
             
     if (Physics.Raycast (rightPos,dwn, 6)) {
     transform.position += Vector3.right;
                 //did we move to an empty space ?
     if (!Physics.Raycast (transform.position,dwn, .5)) {
     okToMove=false;
     InvokeRepeating("dropDown",1.0,1.0);
     
                     
 }
                 
 }
             
 }
 
 insideObject.rotation.eulerAngles.y=90;
 }
 if (Input.GetKeyDown(KeyCode.LeftArrow)) {
 
         
  if (Physics.Raycast (transform.position, l, hit,1)) {
        print ("There is something in front of the object!");
             if(hit.collider.gameObject.tag!="Push"){
     transform.position += Vector3.up;
             }else{
                    print ("Push!");
                   hit.collider.gameObject.transform.position += Vector3.left;
                    transform.position += Vector3.left;
                 
             }
             
     }else{
     Debug.Log("nothing in front");
             
             
     if (Physics.Raycast (leftPos,dwn, 6)) {
     transform.position += Vector3.left;
                 //did we move to an empty space ?
     if (!Physics.Raycast (transform.position,dwn, .5)) {
     okToMove=false;
     InvokeRepeating("dropDown",1.0,1.0);
     
                     
 }
                 
 }
             
 }
 insideObject.rotation.eulerAngles.y=-90;
 }
 }
 
     
     
 
 if (Physics.Raycast (transform.position,dwn,hit, .5)) {
     
     if(hit.collider.gameObject.tag=="HasMission"){
         Debug.Log("has mission");
         if(!showingMission){
             showingMission=true;
             var t:Timer=GameObject.Find("Timer").GetComponent("Timer") as Timer;
             t.startTimer();
             
         }
     }
     
                     
 }
 }
 function dropDown(){
     transform.position += -Vector3.up;
     var dwn = transform.TransformDirection (Vector3.down);
     if (Physics.Raycast (transform.position,dwn, .5)) {
         okToMove=true;
         CancelInvoke();
         
     }
 }
 
 
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
1
Best Answer

Answer by aaronov · May 21, 2013 at 03:40 AM

I'm not really sure what you're asking. You seem to know how to use Raycast so why don't you do a Raycast from your object that represents ending the level to see if it hit the player, then simply do something like:

 Application.LoadLevel(Application.loadedLevel+1);

Alternatively a better approach would be to set your "EndGame" object to be a trigger and do something like:

 void OnTriggerEnter(Collider other) {
     // Move to next level if player hit me
     if (other.gameObject.tag == "Player")
         Application.LoadLevel(Application.loadedLevel+1);
 }
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

14 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

Related Questions

How do I measure how far from the normal a raycast is when hitting a surface? 0 Answers

How can i make the ray cast don't go through the walls 1 Answer

How are raycasts actually implemented? 1 Answer

Want to find the point straight below the controller 1 Answer

enemy Raycast cone detection 0 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