- Home /
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?.
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.
Yes but how do you make it shoot from a gun, like in the game portal?
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.
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.
Thanks for your awnsers but is there like a tutorial to code the portal? Sorry im a new to unity.
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.
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);
}
}
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?
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?
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
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;
}
}
}
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?
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
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.
It works! Thanks for your help! The code you provided works way better then the code I posted.
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
Follow this Question
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