- Home /
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. :(
Can anyone help?!
//
Thank you in advance, Sincerely,
Bakos
ps: I forgot to mention i need the rotation of the object to be on EXACT 90 degree angles. I just tried using:
interF.transform.rotation.y = player.transform.rotation.y;
And it works on rotation the object to FACE me, but does NOT work when it comes to making the object face me on a 90 degree angle.
Check your player.transform.forward vector's direction using Vector3.Dot(Vector3.forward,player.transform.forward)... and so forth, replacing Vector3.forward with all directions you want to snap to.
When used with 2 normalized vectors, Vector3.Dot(a,b) will return a float between 0 and 1, inclusive. The closer to 1 the result is, the closer the two vectors are to pointing the same direction. You can use this to see what direction of up/down/left/right/etc your player is facing. Call Vector3.Dot(a,b) for each direction you could snap to, and then use the direction that gives the result closest to 1.
You can then use Quaternion.LookRotation(closestVector) to get the rotation you want.
Sorry sir, but would you have an example code i could go off of? Or maybe point me in the direction of one?
I have tried using
'Quaternion.LookRotation(player.transform.rotation)'
But to no prevail.
That code doesn't work because you're giving it a Quaternion (ie, mathematician's way of saying "rotation") ins$$anonymous$$d of a vector (ie, a direction with a distance).
For a quick example off the top of my head:
//array of directions you can snap to
Vector3[] snapDirections = new Vector3[]{Vector3.right,Vector3.left,Vector3.forward,Vector3.back};
Vector3 playerForward = player.transform.forward;
Vector3 closestSnapDirection = Vector3.right; //set some default snap direction to start with
float dotProductClosestToOne = $$anonymous$$athf.NegativeInfinity; //used to track the best dot product so far... start off with an impossibly low value
//go through the array and as we go, track which vector
//is closest to pointing the same direction as playerForward
for (int i=0; i<snapDirections.Length; i++)
{
float currDotProduct = Vector3.Dot(snapDirections[i],playerForward);
if (currDotProduct>dotProductClosestToOne)
{
dotProductClosestToOne = currDotProduct;
closestSnapDirection = snapDirections[i];
}
}
//now we've finished this for loop, we've found our snap direction that's closest to the player's forward direction
GameObject.Instantiate(prefab,transform.position,Quaternion.LookRotation(closestSnapDirection));
So i converted it all to Javascript but it seems to be giving me an error with:
var currDotProduct : float = Vector3.Dot(snapDirections[i].playerForward);
Error reads: The best overload for the method 'UnityEngine.Vector3.Dor(UnityEngine.Vector3, UnityEngine.Vector3)' is not compatible with the argument list '(System.Object)'.
I'll upload my entire script in the next answer here if you don't $$anonymous$$d going through it to double check for me.
Answer by bakos · Jan 22, 2014 at 04:39 AM
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[i].playerForward);
if(currDotProduct>dotProductClosestToOne)
{
dotProductClosestToOne = currDotProduct;
closestSnapDirection = snapDirections[i];
}
}/////////////////////////////////////////////////////////////////////
}
}
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;
}
}
}
An easy typo to make - you've written snapDirections[i].playerForward ins$$anonymous$$d of snapDirections[i],playerForward :) Comma, not period.
Also, you'll want to update playerForward's value just before my for loop. It's not a pointer, so it won't ever automatically change its value to match player.transform.forward's current value!
(Better still, make playerForward, closestSnapDirection, and dotProductClosestToOne all variables inside your Build() method - they're not needed anywhere else.)
Still nothing sir :( I've done exactly as you asked sir. No errors when inside the build. But when outside the build, it asks me to assign a value to the buildScript;
I've got all except the snapDirections; inside the build() function. (snapDirections is updating properly to encompass the vector3.left, vector3.right, etc etc. so no big problem there.)
////////////////////////////////////////////////////
var snapDirections : Vector3[];
////////////////////////////////////////////////////
function Update () {
snapDirections[0] = Vector3.right;
snapDirections[1] = Vector3.left;
snapDirections[2] = Vector3.forward;
snapDirections[3] = Vector3.back;
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha0))
{
chosenB = null;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha1))
{
chosenB = blocks[0];
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha2))
{
chosenB = blocks[1];
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha3))
{
chosenB = blocks[2];
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha4))
{
chosenB = blocks[3];
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha5))
{
chosenB = blocks[4];
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha6))
{
chosenB = blocks[5];
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha6))
{
chosenB = blocks[6];
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha7))
{
chosenB = blocks[7];
}
if (Input.Get$$anonymous$$ouseButtonDown(0))
Build();
if (Input.Get$$anonymous$$ouseButtonDown(1))
Erase();
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.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 closestSnapDirection = Vector3.right;
var dotProductClosestToOne = $$anonymous$$athf.NegativeInfinity;
var interF = GameObject.Instantiate(chosenB).transform;
interF.transform.position = hit.transform.position + hit.normal;
interF.transform.tag = "Block";
interF.transform.parent = hit.transform.parent;
var playerForward : Vector3 = player.transform.forward;
for(i = 0; i < snapDirections.Length; i++) ///////////////////////////
{
var currDotProduct : float = Vector3.Dot(snapDirections[i], playerForward);
if(currDotProduct>dotProductClosestToOne)
{
dotProductClosestToOne = currDotProduct;
closestSnapDirection = snapDirections[i];
}
}/////////////////////////////////////////////////////////////////////
}
}
"Only encompasses your stuff.
Is there something i am missing here? :(
I've tried doing as you asked as well, did i declare the player.forward variable at the right point?
Anything there good sir? :)
Still messing around with this whole deal. Very much stuck! :(
Your answer
Follow this Question
Related Questions
Setting quaternion value around local z axis 1 Answer
problem after rotate an object 0 Answers
Rotation based on velocity on one axis 1 Answer
problem after rotate an object 1 Answer
Help platform rotating on x-Axis passes through colliders 0 Answers