position and rotate object based on collision side

Ok so what i’m trying to achieve is a basic portal game so the player can shoot their portals onto walls.

Currently I have got it so when the portal shot collides with a cube in my test scene it will spawn a portal. The problem is the portal is always directly in the center of the cube.
What i want to do is have the portal sit just on the wall and be rotated properly no matter what side or point on the wall it hits.

using UnityEngine;
using System.Collections;

public class bulletControl : MonoBehaviour {

    float speed = 200.0f;
    Vector3 startPoint;
    Vector3 currentPoint;
    public int bulletType = 0;
    GameObject yellowPortal, bluePortal;

	// Use this for initialization
	void Start () {
        startPoint = this.transform.position;
        bluePortal = GameObject.Find("bluePortal");
        yellowPortal = GameObject.Find("yellowPortal");
	// Update is called once per frame
	void Update () {
        this.transform.Translate(Vector3.forward * speed * Time.deltaTime);
        currentPoint = this.transform.position;


    void OnCollisionEnter(Collision collision)
        if(collision.collider.tag == "Target")
            /*if (isNoMoreTargets())
                Debug.Log("yay you completed the game now buy our overpriced DLCs");

            spawnPortal(collision.transform.position , collision.transform.rotation);

    void spawnPortal(Vector3 spawnPosition , Quaternion rotation)
        if(bulletType == 0)
            bluePortal.transform.position = spawnPosition;
            //bluePortal.transform.rotation = rotation;
            yellowPortal.transform.position = spawnPosition;
            //yellowPortal.transform.rotation = rotation;


thanks for any help

Try RayCast, it should give you position of the hit. About rotation… I’m not really sure, but probably you can determine which face was hit (position of the hit should belong to it, use geometry) and determine rotation of that hit (again, use geometry)… but that doesn’t sound like efficient code, and might have problems with hit position being way too colse to edges

You should use Raycast(Physics.Raycast).Raycasts hold the value of normal vector(perpendicular vector of the surface hit by raycast) of the collided surface so you can detect the which face you hit,it will give you direction of your surface depending on the collider’s geometry.
so you should be able to detect your direction of your portal object.About rotation… you have to use your own parameters,you have to observ the outcomes and write a proper rotation scaler.
In my opinion you should setup a few variables which forged with observed outcomes so you can easyly dance between variables and find proper parameters for correct rotation.