Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Portal shoot like a game portal 2

Discussion in 'Editor & General Support' started by EdgarCarrera, Jul 11, 2014.

  1. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Hello guys i want know how i can make a shoot portal Like portal 2 game
     
  2. Flickayy

    Flickayy

    Joined:
    Jan 20, 2013
    Posts:
    40
    You could use a raycast to find the object that you shoot at, then instantiate a texture on the object.

    I'll take a deeper look and see what I can scrape together.
     
    EdgarCarrera likes this.
  3. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    have tutorial for this?
     
  4. Flickayy

    Flickayy

    Joined:
    Jan 20, 2013
    Posts:
    40
    Unfortunately it's not that simple. If you know how to code then you can look through the documentation for raycast, in particular RaycastHit which is used to get information back from said raycast.

    Instantiate is used to clone an object (prefab) which would hold the texture of the portal effect .

    I'm working on a solution, so if you hold on a little bit, I may be able to produce some kind of demo.

    EDIT:

    Here's some code to start you off, not sure if I have the time today to make a demo. If you're still struggling after, I'll take a jab at it when I get back home tonight.

    Code (CSharp):
    1. public RaycastHit hit;
    2. if(Physics.Raycast(origin, direction, hit)){
    3.     instantiatedObject.transform.position = hit.point;
    4. }
     
    Last edited: Jul 11, 2014
    EdgarCarrera likes this.
  5. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255

    look my teleport code i have already like portal 2 my problen now is for shoot! look:

    FillScreen.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FillScreen : MonoBehaviour {
    5.  
    6.     public Camera cam;
    7.  
    8.     public Transform portal1;
    9.     public Camera portal1Cam;
    10.  
    11.     public Transform portal2;
    12.     public Camera portal2Cam;
    13.  
    14.     public Transform sky;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         // Camera.main.depthTextureMode = DepthTextureMode.Depth;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void LateUpdate () {
    23.         //sky.position = cam.transform.position;
    24.      
    25.         Quaternion q = Quaternion.FromToRotation(-portal1.up, cam.transform.forward);
    26.         portal1Cam.transform.position = portal2.position + (cam.transform.position - portal1.position);
    27.         portal1Cam.transform.LookAt(portal1Cam.transform.position + q * portal2.up, portal2.transform.forward);
    28.         portal1Cam.nearClipPlane = (portal1Cam.transform.position - portal2.position).magnitude - 0.3f;
    29.      
    30.         q = Quaternion.FromToRotation(-portal2.up, cam.transform.forward);
    31.         portal2Cam.transform.position = portal1.position + (cam.transform.position - portal2.position);
    32.         portal2Cam.transform.LookAt (portal2Cam.transform.position + q * portal1.up, portal1.transform.forward);
    33.         portal2Cam.nearClipPlane = (portal2Cam.transform.position - portal1.position).magnitude - 0.3f;
    34.      
    35.         Vector3[] scrPoints = new Vector3[4];
    36.         scrPoints[0] = new Vector3(0, 0, 0.1f);
    37.         scrPoints[1] = new Vector3(1, 0, 0.1f);
    38.         scrPoints[2] = new Vector3(1, 1, 0.1f);
    39.         scrPoints[3] = new Vector3(0, 1, 0.1f);
    40.      
    41.         for (int i = 0; i < scrPoints.Length; i++) {
    42.             scrPoints[i] = transform.worldToLocalMatrix.MultiplyPoint(cam.ViewportToWorldPoint(scrPoints[i]));
    43.         }
    44.      
    45.         int[] tris = new int[6] {0, 1, 2, 2, 3, 0};
    46.      
    47.         MeshFilter mf = GetComponent<MeshFilter>();
    48.         mf.mesh.Clear();
    49.         mf.mesh.vertices = scrPoints;
    50.         mf.mesh.triangles = tris;
    51.         mf.mesh.RecalculateBounds();
    52.     }
    53. }
    54.  
    Teleport.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Teleport : MonoBehaviour {
    6.  
    7.     public Transform OtherEnd;
    8.     HashSet<Collider> colliding = new HashSet<Collider>();
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.     }
    19.  
    20.     void OnTriggerEnter(Collider other) {
    21.         if (!colliding.Contains(other)) {
    22.          
    23.          
    24.             Quaternion q1 = Quaternion.FromToRotation(transform.up, OtherEnd.up);
    25.             Quaternion q2 = Quaternion.FromToRotation(-transform.up, OtherEnd.up);
    26.          
    27.             Vector3 newPos = OtherEnd.position + q2 * (other.transform.position - transform.position);// + OtherEnd.transform.up * 2;;
    28.          
    29.             if (other.rigidbody != null) {
    30.                 GameObject o = (GameObject) GameObject.Instantiate(other.gameObject, newPos, other.transform.localRotation);
    31.                 o.rigidbody.velocity = q2 * other.rigidbody.velocity;
    32.                 o.rigidbody.angularVelocity = other.rigidbody.angularVelocity;
    33.                 other.gameObject.SetActive(false);
    34.                 Destroy(other.gameObject);
    35.                 other = o.collider;
    36.             }
    37.          
    38.             OtherEnd.GetComponent<Teleport>().colliding.Add(other);
    39.          
    40.             other.transform.position = newPos;
    41.          
    42.             Vector3 fwd = other.transform.forward;
    43.          
    44.             if (other.rigidbody == null) {
    45.                 other.transform.LookAt(other.transform.position + q2 * fwd, OtherEnd.transform.forward);
    46.             }
    47.         }
    48.     }
    49.  
    50.     void OnTriggerExit(Collider other) {
    51.         colliding.Remove(other);
    52.     }
    53. }
    54.  
    But i have one problem on teleport.

    Example: If i want show this platform like this picture portals:





    if u know or u can fix the script pleasE? very thanks you!
     
  6. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Hello how are u? any news bro?
     
  7. Flickayy

    Flickayy

    Joined:
    Jan 20, 2013
    Posts:
    40
    This is how I reload in my script, however the principle can be applied to your script.


    Code (CSharp):
    1.  void Update() {
    2.         // If the run button is set to toggle, then switch between walk/run speed. (We use Update for this...
    3.         // FixedUpdate is a poor place to use GetButtonDown, since it doesn't necessarily run every frame and can miss the event)
    4.         if (toggleRun && grounded && Input.GetButtonDown("Run"))
    5.             speed = (speed == walkSpeed ? runSpeed : walkSpeed);
    6.  
    7.         // Normal shooting if the clip is not empty,
    8.         // Slowly decreases the ammount of ammo in the current clip.
    9.         if (Time.time > (lastShot + shotdelay)) {
    10.            
    11.             if ((Input.GetKey(KeyCode.Return) || Input.GetMouseButton(0)) && CurrentClipSize >= 1) {
    12.                 if (!isPlaying) {
    13.                     audio.clip = firingSound;
    14.                     audio.Play();
    15.                     weapon.animation.CrossFade("shoot");
    16.                     CurrentClipSize--;
    17.                     lastShot = Time.time;
    18.                     if (TotalAmmo > 0 && CurrentClipSize <= 0) {
    19.                         Reload();
    20.                     }
    21.                 }
    22.             }
    23.             // if the clip is empty but ammo is still left over
    24.             // then force reload.
    25.             if ((Input.GetKey(KeyCode.Return) || Input.GetMouseButton(0)) && CurrentClipSize <= 0) {
    26.                 if (!isPlaying) {
    27.                     if (TotalAmmo > 0) {
    28.                         Reload();
    29.                     }
    30.                 }
    31.             }
    32.         }
    33.         // Manual Reloading
    34.         if (Input.GetKeyUp(KeyCode.R)) {
    35.             if (!isPlaying) {
    36.                 if (CurrentClipSize < 30 && TotalAmmo > 0) {
    37.                     Reload();
    38.                 }
    39.             }
    40.         }
    41.     }
    42.  
    43.     // Store point that we're in contact with for use in FixedUpdate if needed
    44.     void OnControllerColliderHit(ControllerColliderHit hit) {
    45.         contactPoint = hit.point;
    46.     }
    47.  
    48.     // If falling damage occured, this is the place to do something about it. You can make the player
    49.     // have hitpoints and remove some of them based on the distance fallen, add sound effects, etc.
    50.     void FallingDamageAlert(float fallDistance) {
    51.         print("Ouch! Fell " + fallDistance + " units!");
    52.     }
    53.  
    54.     // Reload the current weapon.
    55.     // TODO: Allow for an array of clip sizes and total ammo.
    56.     void Reload() {
    57.         audio.clip = reloadSound;
    58.         audio.Play();
    59.         if (TotalAmmo > 30) {
    60.             StartCoroutine(ReloadAnimation());
    61.             if (CurrentClipSize < 30 && TotalAmmo > 0) {
    62.                 TotalAmmo = TotalAmmo - (ClipSize - CurrentClipSize);
    63.                 CurrentClipSize = ClipSize;
    64.  
    65.             }
    66.             if (CurrentClipSize == 0 && TotalAmmo > 0) {
    67.                 TotalAmmo = TotalAmmo - ClipSize;
    68.                 CurrentClipSize = ClipSize;
    69.             }
    70.             if (CurrentClipSize <= 0 && TotalAmmo <= 0) {
    71.                 CurrentClipSize = 0;
    72.                 Debug.Log("Out of ammo");
    73.             }
    74.         }
    75.         else if ( TotalAmmo == 0 && CurrentClipSize == 0) {
    76.             return;
    77.         }
    78.         else {
    79.             StartCoroutine(ReloadAnimation());
    80.             CurrentClipSize = TotalAmmo;
    81.             TotalAmmo = 0;
    82.         }
    83.      
    84.     }
    I would suggest that you have a look at some tutorials on how to instantiate bullets, however all you would do then is replace the bullet, and create a ray-cast from the mesh and then check if it hits a tagged mesh.
     
    Last edited: Jul 13, 2014
  8. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    but this is for teleport?
     
  9. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255

    why u send me this script?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(CharacterController))]
    5. [RequireComponent(typeof(AudioSource))]
    6. [AddComponentMenu("Controllers/Player Controller")]
    7. public class PlayerController : MonoBehaviour {
    8.    
    9.     #region  .Member Variables.
    10.     public bool isPlaying = false;
    11.     public float lastShot = .0f;
    12.     public float shotdelay = .5f;
    13.     public float walkSpeed = 6.0f;
    14.     public float runSpeed = 11.0f;
    15.     public GameObject weapon;
    16.     public AudioClip cockingSound, firingSound, reloadSound;
    17.    
    18.     public int ClipSize = 30;
    19.     public int TotalAmmo = 180;
    20.     public int CurrentClipSize = 30;
    21.    
    22.     // If true, diagonal speed (when strafing + moving forward or back) can't exceed normal move speed; otherwise it's about 1.4 times faster
    23.     public bool limitDiagonalSpeed = true;
    24.    
    25.     // If checked, the run key toggles between running and walking. Otherwise player runs if the key is held down and walks otherwise
    26.     // There must be a button set up in the Input Manager called "Run"
    27.     public bool toggleRun = false;
    28.     public float turnSpeed = 60f;
    29.     public float jumpSpeed = 8.0f;
    30.     public float gravity = 20.0f;
    31.    
    32.     // Units that player can fall before a falling damage function is run. To disable, type "infinity" in the inspector
    33.     public float fallingDamageThreshold = 10.0f;
    34.    
    35.     // If the player ends up on a slope which is at least the Slope Limit as set on the character controller, then he will slide down
    36.     public bool slideWhenOverSlopeLimit = false;
    37.    
    38.     // If checked and the player is on an object tagged "Slide", he will slide down it regardless of the slope limit
    39.     public bool slideOnTaggedObjects = true;
    40.     public float slideSpeed = 12.0f;
    41.    
    42.     // If checked, then the player can change direction while in the air
    43.     public bool airControl = true;
    44.    
    45.     // Small amounts of this results in bumping when walking down slopes, but large amounts results in falling too fast
    46.     public float antiBumpFactor = .75f;
    47.    
    48.     // Player must be grounded for at least this many physics frames before being able to jump again; set to 0 to allow bunny hopping
    49.     public int antiBunnyHopFactor = 1;
    50.    
    51.     private Vector3 moveDirection = Vector3.zero;
    52.     private bool grounded = false;
    53.     private CharacterController controller;
    54.     private Transform myTransform;
    55.     private float speed;
    56.     private RaycastHit hit;
    57.     private float fallStartLevel;
    58.     private bool falling;
    59.     private float slideLimit;
    60.     private float rayDistance;
    61.     private Vector3 contactPoint;
    62.     private bool playerControl = false;
    63.     private int jumpTimer;
    64.     #endregion
    65.    
    66.     // Called on creation of the Player GameObject (in-game)
    67.     void Awake() {
    68.         controller = GetComponent<CharacterController>();
    69.         audio.clip = cockingSound;
    70.         myTransform = transform;
    71.         speed = walkSpeed;
    72.         rayDistance = controller.height * .5f + controller.radius;
    73.         slideLimit = controller.slopeLimit - .1f;
    74.         jumpTimer = antiBunnyHopFactor;
    75.        
    76.     }
    77.    
    78.     void Start() {
    79.         // Start the drawn weapon animation and play
    80.         // audio with it.
    81.         StartCoroutine(DrawAnimation());
    82.         if(isPlaying)
    83.             audio.Play();
    84.     }
    85.    
    86.     // Called at a fixed rate (Physic based updates)
    87.     void FixedUpdate() {
    88.         float inputX = Input.GetAxis("Horizontal");
    89.         float inputY = Input.GetAxis("Vertical");
    90.         // If both horizontal and vertical are used simultaneously, limit speed (if allowed), so the total doesn't exceed normal move speed
    91.         float inputModifyFactor = (inputX != 0.0f && inputY != 0.0f && limitDiagonalSpeed) ? .7071f : 1.0f;
    92.         transform.Rotate(0, inputX * turnSpeed * Time.deltaTime, 0);
    93.         if (grounded) {
    94.             bool sliding = false;
    95.             // See if surface immediately below should be slid down. We use this normally rather than a ControllerColliderHit point,
    96.             // because that interferes with step climbing amongst other annoyances
    97.             if (Physics.Raycast(myTransform.position, -Vector3.up, out hit, rayDistance)) {
    98.                 if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
    99.                     sliding = true;
    100.             }
    101.             // However, just raycasting straight down from the center can fail when on steep slopes
    102.             // So if the above raycast didn't catch anything, raycast down from the stored ControllerColliderHit point instead
    103.             else {
    104.                 Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, out hit);
    105.                 if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
    106.                     sliding = true;
    107.             }
    108.            
    109.             // If we were falling, and we fell a vertical distance greater than the threshold, run a falling damage routine
    110.             if (falling) {
    111.                 falling = false;
    112.                 if (myTransform.position.y < fallStartLevel - fallingDamageThreshold)
    113.                     FallingDamageAlert(fallStartLevel - myTransform.position.y);
    114.             }
    115.            
    116.             // If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down
    117.             if (!toggleRun)
    118.                 speed = Input.GetButton("Run") ? runSpeed : walkSpeed;
    119.            
    120.             // If sliding (and it's allowed), or if we're on an object tagged "Slide", get a vector pointing down the slope we're on
    121.             if ((sliding && slideWhenOverSlopeLimit) || (slideOnTaggedObjects && hit.collider.tag == "Slide")) {
    122.                 Vector3 hitNormal = hit.normal;
    123.                 moveDirection = new Vector3(hitNormal.x, -hitNormal.y, hitNormal.z);
    124.                 Vector3.OrthoNormalize(ref hitNormal, ref moveDirection);
    125.                 moveDirection *= slideSpeed;
    126.                 playerControl = false;
    127.             }
    128.             // Otherwise recalculate moveDirection directly from axes, adding a bit of -y to avoid bumping down inclines
    129.             else {
    130.                 moveDirection = new Vector3(inputX * inputModifyFactor, -antiBumpFactor, inputY * inputModifyFactor);
    131.                 moveDirection = myTransform.TransformDirection(moveDirection) * speed;
    132.                 playerControl = true;
    133.             }
    134.            
    135.             // Jump! But only if the jump button has been released and player has been grounded for a given number of frames
    136.             if (!Input.GetButton("Jump"))
    137.                 jumpTimer++;
    138.             else if (jumpTimer >= antiBunnyHopFactor) {
    139.                 moveDirection.y = jumpSpeed;
    140.                 jumpTimer = 0;
    141.             }
    142.         }
    143.         else {
    144.             // If we stepped over a cliff or something, set the height at which we started falling
    145.             if (!falling) {
    146.                 falling = true;
    147.                 fallStartLevel = myTransform.position.y;
    148.             }
    149.            
    150.             // If air control is allowed, check movement but don't touch the y component
    151.             if (airControl && playerControl) {
    152.                 moveDirection.x = inputX * speed * inputModifyFactor;
    153.                 moveDirection.z = inputY * speed * inputModifyFactor;
    154.                 moveDirection = myTransform.TransformDirection(moveDirection);
    155.             }
    156.         }
    157.        
    158.         // Apply gravity
    159.         moveDirection.y -= gravity * Time.deltaTime;
    160.        
    161.         // Move the controller, and set grounded true or false depending on whether we're standing on something
    162.         grounded = (controller.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0;
    163.     }
    164.    
    165.     // Called every frame
    166.     void Update() {
    167.         // If the run button is set to toggle, then switch between walk/run speed. (We use Update for this...
    168.         // FixedUpdate is a poor place to use GetButtonDown, since it doesn't necessarily run every frame and can miss the event)
    169.         if (toggleRun && grounded && Input.GetButtonDown("Run"))
    170.             speed = (speed == walkSpeed ? runSpeed : walkSpeed);
    171.        
    172.         // Normal shooting if the clip is not empty,
    173.         // Slowly decreases the ammount of ammo in the current clip.
    174.         if (Time.time > (lastShot + shotdelay)) {
    175.            
    176.             if ((Input.GetKey(KeyCode.Return) || Input.GetMouseButton(0)) && CurrentClipSize >= 1) {
    177.                 if (!isPlaying) {
    178.                     audio.clip = firingSound;
    179.                     audio.Play();
    180.                     weapon.animation.CrossFade("shoot");
    181.                     CurrentClipSize--;
    182.                     lastShot = Time.time;
    183.                     if (TotalAmmo > 0 && CurrentClipSize <= 0) {
    184.                         Reload();
    185.                     }
    186.                 }
    187.             }
    188.             // if the clip is empty but ammo is still left over
    189.             // then force reload.
    190.             if ((Input.GetKey(KeyCode.Return) || Input.GetMouseButton(0)) && CurrentClipSize <= 0) {
    191.                 if (!isPlaying) {
    192.                     if (TotalAmmo > 0) {
    193.                         Reload();
    194.                     }
    195.                 }
    196.             }
    197.         }
    198.         // Manual Reloading
    199.         if (Input.GetKeyUp(KeyCode.R)) {
    200.             if (!isPlaying) {
    201.                 if (CurrentClipSize < 30 && TotalAmmo > 0) {
    202.                     Reload();
    203.                 }
    204.             }
    205.         }
    206.     }
    207.    
    208.     // Store point that we're in contact with for use in FixedUpdate if needed
    209.     void OnControllerColliderHit(ControllerColliderHit hit) {
    210.         contactPoint = hit.point;
    211.     }
    212.    
    213.     // If falling damage occured, this is the place to do something about it. You can make the player
    214.     // have hitpoints and remove some of them based on the distance fallen, add sound effects, etc.
    215.     void FallingDamageAlert(float fallDistance) {
    216.         print("Ouch! Fell " + fallDistance + " units!");
    217.     }
    218.    
    219.     // Reload the current weapon.
    220.     // TODO: Allow for an array of clip sizes and total ammo.
    221.     void Reload() {
    222.         audio.clip = reloadSound;
    223.         audio.Play();
    224.         if (TotalAmmo > 30) {
    225.             StartCoroutine(ReloadAnimation());
    226.             if (CurrentClipSize < 30 && TotalAmmo > 0) {
    227.                 TotalAmmo = TotalAmmo - (ClipSize - CurrentClipSize);
    228.                 CurrentClipSize = ClipSize;
    229.                
    230.             }
    231.             if (CurrentClipSize == 0 && TotalAmmo > 0) {
    232.                 TotalAmmo = TotalAmmo - ClipSize;
    233.                 CurrentClipSize = ClipSize;
    234.             }
    235.             if (CurrentClipSize <= 0 && TotalAmmo <= 0) {
    236.                 CurrentClipSize = 0;
    237.                 Debug.Log("Out of ammo");
    238.             }
    239.         }
    240.         else if ( TotalAmmo == 0 && CurrentClipSize == 0) {
    241.             return;
    242.         }
    243.         else {
    244.             StartCoroutine(ReloadAnimation());
    245.             CurrentClipSize = TotalAmmo;
    246.             TotalAmmo = 0;
    247.         }
    248.        
    249.     }
    250.    
    251.     // Reload animation co-routine
    252.     IEnumerator ReloadAnimation() {
    253.         weapon.animation.CrossFade("reload");
    254.         isPlaying = true;
    255.         yield return new WaitForSeconds(weapon.animation["reload"].length); // wait for two seconds.
    256.         isPlaying = false;
    257.     }
    258.    
    259.     // Weapon Drawn animation co-routine
    260.     IEnumerator DrawAnimation() {
    261.         weapon.animation.CrossFade("draw_001");
    262.         isPlaying = true;
    263.         yield return new WaitForSeconds(weapon.animation["draw_001"].length); // wait for aniamtion to complete
    264.         isPlaying = false;
    265.     }
    266.    
    267. }
     
  10. Flickayy

    Flickayy

    Joined:
    Jan 20, 2013
    Posts:
    40
    There's no need to double post....

    I sent you the script so you could understand how a keydown event is processed. I'm not going to write your scripts for you, as I have my own projects to attend to. I suggest going back and studying some more tutorials if you can't get past this.