Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bakos · Jan 22, 2014 at 03:05 AM · rotate objecteulerlocal axis

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

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bakos · Jan 22, 2014 at 03:15 AM 0
Share

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.

avatar image nesis · Jan 22, 2014 at 03:20 AM 0
Share

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.

avatar image bakos · Jan 22, 2014 at 03:34 AM 0
Share

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.

avatar image nesis · Jan 22, 2014 at 04:00 AM 0
Share

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));
avatar image bakos · Jan 22, 2014 at 04:38 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
              
         }
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nesis · Jan 22, 2014 at 04:49 AM 0
Share

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.)

avatar image bakos · Jan 22, 2014 at 10:20 AM 0
Share

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?

avatar image bakos · Jan 23, 2014 at 03:13 AM 0
Share

Anything there good sir? :)

Still messing around with this whole deal. Very much stuck! :(

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges