Instantiate object at hit.normal facing the same direction as my player.

Hello everyone!

I am back with another puzzling question :D!

//Question://

I am building a minecraft style game, and i am able to instantiate an object on the hit.normal of another. So all the placement stuff works. Only thing is, my player is able to rotate 360 degrees on any axis so if i try to attach a:

var interF : GameObject.Instantiate(object, transform.position, player.transform.rotation);

it will rotate the object to face me DIRECTLY on any angle; So if i’m 45 degrees above my object and i look down at it and place an object, the front of the object will face my camera, which i do not want.

What i need is for it to just face me along only the y axis.

So if i instantiate the object from a 45 degree angle, it will stand upright, and face towards my direction.

What i think i need is a way to instantiate my object along a eulerAngle.y, but i cannot think of how to do this. :frowning:

Can anyone help?!

//

Thank you in advance,
Sincerely,

Bakos

Everything you wrote is between “////////////////”.

var blockLayer : LayerMask = 1;
var range : float = 100;
var hit : RaycastHit;

var blocks : GameObject[];
var chosenB : GameObject;

static var shipStart : boolean;
var shipBegin : GameObject;
var shipParent : Transform;

var player : Transform;
var instantiatedObject;
var isC = false;
////////////////////////////////////////////////////
var snapDirections : Vector3[];
var playerForward : Vector3 = player.transform.forward;

var closestSnapDirection = Vector3.right;
var dotProductClosestToOne = Mathf.NegativeInfinity;
////////////////////////////////////////////////////

function Update () {	

	if(Input.GetKeyDown(KeyCode.Alpha0))
	{
		chosenB = null;
	}
	if(Input.GetKeyDown(KeyCode.Alpha1))
	{
		chosenB = blocks[0];
	}
	if(Input.GetKeyDown(KeyCode.Alpha2))
	{
		chosenB = blocks[1];
	}
	if(Input.GetKeyDown(KeyCode.Alpha3))
	{
		chosenB = blocks[2];
	}
	if(Input.GetKeyDown(KeyCode.Alpha4))
	{
		chosenB = blocks[3];
	}
	if(Input.GetKeyDown(KeyCode.Alpha5))
	{
		chosenB = blocks[4];
	}
	if(Input.GetKeyDown(KeyCode.Alpha6))
	{
		chosenB = blocks[5];
	}
	if(Input.GetKeyDown(KeyCode.Alpha6))
	{
		chosenB = blocks[6];
	}
	if(Input.GetKeyDown(KeyCode.Alpha7))
	{
		chosenB = blocks[7];
	}
	
	
    if (Input.GetMouseButtonDown(0))
        Build();
    if (Input.GetMouseButtonDown(1))
        Erase();
    
	
	if(Input.GetKeyDown(KeyCode.B))
	{
		shipStart = !shipStart;
		isC = true;
	}
	
    if (Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer)) 
    {
        chosenB.transform.position = hit.normal;
    }
	
}
 
function Build() {

    if (HitBlock() && hit.transform.tag == "Block") 
    {
    	
        var interF = GameObject.Instantiate(chosenB).transform;
        interF.transform.position = hit.transform.position + hit.normal;
        interF.transform.tag = "Block";
        interF.transform.parent = hit.transform.parent;
        for(i = 0; i < snapDirections.Length; i++) ///////////////////////////
        {
        	var currDotProduct : float = Vector3.Dot(snapDirections*.playerForward);*
  • if(currDotProduct>dotProductClosestToOne)*
  • {*
  •  dotProductClosestToOne = currDotProduct;*
    

_ closestSnapDirection = snapDirections*;_
_
}_
_
}/////////////////////////////////////////////////////////////////////_
_
}_
_
}*_

function Erase() {

* if (HitBlock() && hit.transform.GetComponentInChildren(activateDeact))*
* {*
* return null;*
}
else if (HitBlock() && hit.transform.tag == “Block”)
Destroy(hit.transform.gameObject);
}

function HitBlock() : boolean {
* return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);*
}

function OnGUI()
{

* if(shipStart == true)*
* {*

* if(GUI.Button(Rect(1,1,100,20), “Create Ship”))*
* {*
* var ship = Instantiate(shipBegin, transform.position, Quaternion.identity);*
* shipP = Instantiate(shipParent, transform.position, Quaternion.identity);*
* ship.transform.tag = “Block”;*
* ship.transform.parent = shipP.transform;*

* }*
* }*
}