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

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.

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;
		}
	}
}