Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 lonelycamper · Feb 25, 2018 at 06:12 PM · quaternionbuilding

rotating the object in the correct orientation

Hi im wondering how to get my objects rotation right

I was using my preview objects rotation but if were to rotate it I would have to use quaternion euler

but when i use it this happens: link

the object is rotated in the wrong orientation no matter what I do

My script public void ChangeCurrentsBuilding()

     {
         //GameObject curprev = Instantiate(currentobject.preview, currentpos, currentobject.preview.transform.rotation) as GameObject;
         GameObject curprev = Instantiate(currentobject.preview, currentpos, Quaternion.Euler(currentRot)) as GameObject;
         currentpreview = curprev.transform;
     }
 
     public void startPreview()
 
 
     {
 
         if (Physics.Raycast(cam.position, cam.forward, out hit, 50, layer)) {
 
             if (hit.transform != this.transform) {
 
 
                 showPreview(hit);
 
             }
 
         }
 
     }
 
     public void showPreview(RaycastHit hit2) {
 
         currentpos = hit2.point;
         currentpos -= Vector3.one * offset;
         currentpos /= gridSize;
         currentpos = new Vector3(Mathf.Round(currentpos.x), Mathf.Round(currentpos.y), Mathf.Round(currentpos.z));
         currentpos *= gridSize;
         currentpos += Vector3.one * offset;
         currentpreview.position = currentpos;
         if (Input.GetButtonDown("Fire2")) {
 
             currentRot += new Vector3(0, 90, 0);
 
             currentpreview.localEulerAngles = currentRot;
 
         }
 
     }
 
 
 
 
     public void Build()
     {
         PreviewObject PO = currentpreview.GetComponent<PreviewObject>();
         if(PO.isBuildable)
         {
 
             Instantiate(currentobject.prefab, currentpos, Quaternion.Euler(currentRot));
 
 
         }
 
 
 }
 }
 
 [System.Serializable]
 
 
 public class buildObjects {
 
     public string name;
     public GameObject preview;
     public GameObject prefab;
     public int gold;
 
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Glurth · Feb 25, 2018 at 06:53 PM

 currentpreview.localEulerAngles = currentRot;

This line will affect the rotation of the object, but only relative to it's parent. If the parent-object is rotated about say... the X axis, then adjusting the child's localRotation about the Y axis, will NOT change the object's rotation around the X axis. However, if you were to modify the object's world-space "rotation"

 currentpreview.rotation = Quaternion.Euler(currentRot);

This will set the orientation relative to the WORLD (as opposed to relative to the parent).

I mention this because in the images it looks like the "correct" version is rotated about the X (or Z) axis, but the "incorrect" one is not. So, you either need a parent object that is roatated about X, OR, you need to specify that rotation in your initial Euler angle (initial, meaning: don't add it again everytime you rotate!).

Comment
Add comment · Show 5 · 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 lonelycamper · Feb 25, 2018 at 07:09 PM 0
Share

so how do i specify the rotation in my initial euler angle or how do i parent the preview object

avatar image Glurth lonelycamper · Feb 25, 2018 at 07:24 PM 0
Share

"how do i specify the rotation in my initial euler angle" You can do this in the when you instantiate the object, simply adjust the quaternion passed to the Instantiate Function:

 GameObject curprev = Instantiate(currentobject.preview, currentpos,Quaternion.Euler(90,0,0)) as GameObject;
 currentpreview = curprev.transform;

To Parent the object: Create an empty-gameobject before creating your object, then pass that empty-gameobject to the instantiate function:

 GameObject parent= new GameObject();
 parent.transform.rotation=Quaternion.Euler(90,0,0);
 GameObject curprev = Instantiate(currentobject.preview, currentpos, Quaternion.Euler(currentRot), parent) as GameObject;

avatar image lonelycamper Glurth · Feb 25, 2018 at 07:44 PM 0
Share

It works but when i rotate it doesnt rotate on the correct axis tho

Show more comments

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

75 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 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 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 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

Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers

Rotation issue with remote players projectiles 0 Answers

How to check for NaN 4 Answers

My Raycast dont hit. 3 Answers

How to mirror limb movement 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