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 RandomUser123 · Sep 13, 2015 at 05:03 PM · rotationinstantiate

Instantiating an object with the correct rotation

I would like to instantiate an object with the correct rotation but unsure about how I'd do it. If i Instantiate the object on a flat surface it looks fine but if I Instantiate on a vertical or sloped wall, it looks weird. Take the below image as an example:

alt text

The slime on the right is flat on the ground but the one on the left is in the same rotation but sticking from the wall. Is there a way I can get the object to like against the wall for a more natural look? This is the code I'm currently using:

EDIT

I found a post here that says I should rotate according to the normals of the object which makes sense but I am still having no luck using the following code:

     RaycastHit hit;
     Vector3 dir = -transform.up;
     float length = 0.35f;

     Debug.DrawRay(transform.position, dir * length, Color.green);

     if (Physics.Raycast(transform.position, dir * length, out hit, length))
     {
         SpawnObject(null, manager.slimeBalls, transform.position, Quaternion.LookRotation(hit.normal));
     }

Just for reference, this is the SpawnObject code I am using:

 void SpawnObject(Collider col, List<GameObject> objectList, Vector3 position, Quaternion rotation)
     {
         //For each artifact in the list
         for (int i = 0; i < objectList.Count; i++)
         {
             //If a artifact is inactive
             if (!objectList[i].activeInHierarchy)
             {
                 //Set the position to the collision point
                 objectList[i].transform.position = position;
 
                 //Set the rotation to the rotation collision point
                 objectList[i].transform.rotation = rotation;
 
                 //Set the artifact to active
                 objectList[i].SetActive(true);
 
                 //break from the loop
                 break;
             }
         }
     }

capture.png (343.7 kB)
Comment
Add comment · Show 2
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 Positive7 · Sep 13, 2015 at 05:21 PM 0
Share

You could use a Raycast downwards of the splash and use hit.normal for it's rotation.

Something like :

 Ray ray = new Ray (transform.position, Vector3.down);
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit, 10)) {
             Debug.Log("Hit Point : " + hit);
             Vector3 hitPoint = hit.point;
             Quaternion vnorm = new Quaternion(hit.normal.z, hit.normal.y, -hit.normal.x, 1);
             Instantiate (splash, hitPoint, vnorm);
             
         }

I'm not sure if it will work for you, but it worth a try.

avatar image RandomUser123 Positive7 · Sep 13, 2015 at 06:24 PM 0
Share

@Positive7 thanks for the response but no luck with that unfortunately, I've added a bit more to my question

1 Reply

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

Answer by Eno-Khaon · Sep 13, 2015 at 07:14 PM

At the moment, you're telling your object's "forward" direction to orient itself facing outward from the surface it's sitting on.

To get the rotation you're trying for, you need to add a small, extra step to your process.

Using FromToRotation(), you can adapt your rotation into a small tweak rather than a large change in direction.

 Quaternion newDirection = Quaternion.FromToRotation(transform.up, hit.normal);
 
 SpawnObject(null, manager.slimeBalls, transform.position, newDirection);

On this basis, the newDirection would be attained as a relative rotation to the spawner. As an alternative, "Vector3.up" could be used as an alternative to "transform.up" in case the spawner would be rotated.

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 RandomUser123 · Sep 13, 2015 at 08:37 PM 0
Share

Thanks, i will give this a shot when I get home

avatar image RandomUser123 · Sep 13, 2015 at 11:25 PM 0
Share

@$$anonymous$$o $$anonymous$$haon I don't know why but I can't this to work at all, nothing is happening with the new code unfortunately, am I missing something to this?

 void Update()
      {
          RaycastHit hit;
          Vector3 dir = -transform.up;
          float length = 0.35f;
 
  
      if (Physics.Raycast(transform.position, dir, out hit, length) && !running)
      {
          running = true;
          Quaternion newRotation = Quaternion.FromToRotation(transform.up, hit.normal);
          SpawnObject(null, manager.slimeBalls, transform.position, newRotation);
          running = false;
          gameObject.SetActive(false);
      }
  }


avatar image RandomUser123 · Sep 13, 2015 at 11:41 PM 0
Share

I got it in the end, the Animator on the object seemed to be freezing its rotation, i disabled the animator and everything works fine now, thanks for the answer :)

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

32 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

Related Questions

How to rotate an Instantiated object? 1 Answer

Why is my projectile firing "off" after rotation 1 Answer

Changing build rotation 2 Answers

How to intantiate an object in front of a rotating object 1 Answer

A way to instantiate an item with 90 Degree increments? 2 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