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 mattglas · May 13, 2013 at 11:31 PM · fpsportal

Is it posable to make a gun shoot a portal?

Im trying to make a game like portal but having problems with a script. So does anyone know how to have a gun shoot a portal and then the player walk through to another portal?.

Comment
Add comment · Show 7
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 rutter · May 13, 2013 at 11:36 PM 1
Share

Depends on what you want that portal to do.

Portal that, when touched, instantly teleports the player to another location? Pretty easy. You can set up a collider/trigger component representing the portal, set up a script that checks for the player hitting the portal, and have it set the player's transform to whatever location you desire. Add flashy graphics as needed.

Portal that allows robust physics and vision back and forth between entry/exit points? Considerably more difficult.

avatar image mattglas · May 14, 2013 at 02:00 AM 0
Share

Yes but how do you make it shoot from a gun, like in the game portal?

avatar image Fornoreason1000 · May 14, 2013 at 02:04 AM 0
Share

easy make you "portal" object a make it shoot like you would with a bullet, and make it stop when it hits something or a certain distance away. the only thing that's substantially different from your gun and a regular machine gun is that your using portals ins$$anonymous$$d of bullets.

avatar image Jarsh13 · May 14, 2013 at 03:01 AM 0
Share

the problem with portal isnt the teleporting part its the effect of looking through the portal. Render to texture is needed to do that I believe.

avatar image mattglas · May 14, 2013 at 09:46 PM 0
Share

Thanks for your awnsers but is there like a tutorial to code the portal? Sorry im a new to unity.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Negagames · May 14, 2013 at 10:00 PM

Is it possible? Yes, just about anything is possible when it comes to game dev :), i'm not going to write out a lengthy tutorial on how. But i'll give you some hints.

  • Portals could be instantiated. So when you shoot your portal bullet, a portal object is created upon collision w/ another object.

  • Objects can detect other objects using gameObject.FindWithTag(TagName); so you could have to disconnected portal entities recognize each other. Which allows for disambiguation of script. If you need anymore, just comment.

Comment
Add comment · Show 4 · 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 mattglas · May 15, 2013 at 12:32 AM 0
Share

Thanks for your replay. Anyways i was able to get a portal to show up on a wall when i fired a gun, and taking $$anonymous$$ibsgaard advice and went to http://answers.unity3d.com/questions/60709/portal-physics-effect.html. But i still cant get a object to go from one portal to another. I also tried tags but could not get them to work.

Here is the code:

 public var ignoreTime : float = 1;
  
 function OnTriggerStay(c : Collider) {
     if(c.CompareTag("PortalA") || c.CompareTag("PortalB")) {
         var otherPortal : Transform = GameObject.FindGameObjectWithTag(c.CompareTag("PortalA")?"PortalB":"PortalA").transform;
         transform.localEulerAngles += Quaternion.LookRotation(otherPortal.forward).eulerAngles;
         transform.position = otherPortal.TransformDirection(Vector3.Reflect(c.transform.position - transform.position, c.transform.forward)) + otherPortal.position;
         rigidbody.velocity = otherPortal.TransformDirection(c.transform.InverseTransformDirection(Vector3.Reflect(rigidbody.velocity,c.transform.forward)));
         Physics.IgnoreCollision(collider,otherPortal.collider,true);
         yield WaitForSeconds(ignoreTime);
         Physics.IgnoreCollision(collider,otherPortal.collider,false);
     }
 }
avatar image Negagames · May 15, 2013 at 02:13 AM 0
Share

Ok, so have an onTriggerEnter function on your portal object, and when an object comes in contact:

 var portal2 : GameObject;
 
 function OnTriggerEnter(col : Collider){//check for objects
     if(col.tag == "player"){//It's the player! transport them
         col.transform.position = portal2.transform.position;
     }
 }
 
 
 function Start(){
     portal2 = gameObject.FindWithTag(portal);//Find the corrosponding portal, instantiate it how ever you want! As long as it has the tag portal
 }

solved?

avatar image mattglas · May 15, 2013 at 08:21 PM 0
Share

So i add this code to both portals and made a rigidbody sphere above it with the tag player. But i get the error: Assets/portal.js(11,37): BCE0017: The best overload for the method 'UnityEngine.GameObject.FindWithTag(String)' is not compatible with the argument list '(System.Type)'.

Any idea how to fix?

avatar image Fornoreason1000 · May 15, 2013 at 11:24 PM 0
Share

portal needs to be a string

 portal2 = gameObject.FindWithTag("portal");//Find the corrosponding portal, instantiate it how ever you want! As long as it has the tag portal
avatar image
1

Answer by Chronos-L · May 16, 2013 at 02:24 AM

Addition to @mattglas's answer

This is an alternative answer, and at the same time, an addition to @matglass's answer.

 using UnityEngine;
 using System.Collections;
  
 public class MakePortal : MonoBehaviour {
     public Portal prefabBlue, prefabYellow;
     
     private Portal a, b;
     
     void Start() {
         a = null;
         b = null;
     }
     
     void Update () {
         if(Input.GetMouseButtonDown(0))
         {
             if( Make( prefabBlue, ref a ) && b != null ) {
                 a.another = b;
                 b.another = a;
             }
         }
         else if(Input.GetMouseButtonDown(1))
         {
             if( Make( prefabYellow, ref b ) && a != null ) {
                 a.another = b;
                 b.another = a;
             }
         }
     }
     
     private bool Make( Portal prefab, ref Portal assign ) {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         
         if (Physics.Raycast(ray, out hit)) {
         
             //Use some condition/calculation to check if you can make this portal
             // - Condition 1: eg. Will the new blue portal overlap with the existing yellow portal?
             // - Condition 2: can the surface be used to make portal? ( hit.transform.CompareTag("PortalReadySurface") )
             // - other conditions...
             bool canMakePortal = ...;
             
             if( canMakePortal) {
                 Vector3 position = hit.point + 0.5f * hit.normal; //Make the portal slightly elevated from the surface
                 Quaternion rotation = Quaternion.LookRotation( hit.normal ); //Aligning the portal, this should be right
                 
                 if( assign == null ) {
                     assign = Instantiate( prefab, position, rotation ) as Portal;
                 }
                 else {
                     assign.transform.position = position;
                     assign.transform.rotation = rotation;
                 }
                 
                 return true; //Make a portal successfully
             }
             else {
                 //Optional: If the previous portal needed to be destroy whenever you hit a surface
                 //It is been a while since I played Portal, don't really remember the behaviour any more
                 Destroy( assign );
             }
         }
         
         return false;
     }    
 }


 using UnityEngine;
 using System.Collections;
  
 public class Portal : MonoBehaviour {
  
     public Portal another;
     
     void OnTriggerEnter( Collider col)
     {
         if ( col.transform.CompareTag("Player") && another != null )
         {
             //Reposition the player in front of another portal
             col.transform.position = another.transform.position + another.transform.forward * 1.5f;
         }
     }
 }
Comment
Add comment · Show 5 · 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 mattglas · May 17, 2013 at 01:59 AM 0
Share

I get this error when running your script:

NullReferenceException UnityEngine.Camera.ScreenPointToRay (Vector3 position) $$anonymous$$akePortal.$$anonymous$$ake (.Portal prefab, .Portal& assign) (at Assets/Scripts/$$anonymous$$akePortal.cs:32) $$anonymous$$akePortal.Update () (at Assets/Scripts/$$anonymous$$akePortal.cs:24)

Any Ideas?

avatar image Chronos-L · May 17, 2013 at 08:44 AM 0
Share

This line:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

is returning a null-ref exception.

Do you have a main-camera in the scene? Check if you have a camera that meet the specifications

avatar image Chronos-L · May 17, 2013 at 08:46 AM 0
Share

Attach the $$anonymous$$akePortal.cs to any active gameobject, preferably the player or the camera. Attach the Portal.cs to the portal object with the portal material, texture and stuff.

avatar image mattglas · May 17, 2013 at 08:46 PM 0
Share

It works! Thanks for your help! The code you provided works way better then the code I posted.

avatar image Chronos-L · May 18, 2013 at 02:02 AM 0
Share

It is based on the code/logic you provided. What I have done are simplifying the code/logic and do some clean-up to complete the whole thing.

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

Whow do i change my character height 1 Answer

My distance variable is not changing? 2 Answers

Problem with Javascript updating variables. 1 Answer

Muzzleflash help 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