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 cheesydongboys · Feb 24, 2017 at 09:46 AM · raycastinstantiate prefab

Issues with instantiated prefabs transform using hit.point?

Hello,

I am running a simple test scene trying to instantiate a prefab composed of a large number of small cubes at the hit point of a raycast. The scene is composed of a simple cube falling towards a plane. As the cube falls a raycast is used to check the tag of the plane below. Based on this tag the physic material of the cube is changed. In this case if the tag is set to 0, a prefab composed of 2000+ cubes is created at the hit.point of the raycast. Currently the raycast works and the cubes are created but I can't seem to get the position and rotation of the cubes correct. The script I am using and some pictures of the issues can be seen attached. The script is just attached to the falling cube object. I have tried a couple different position and quaternion inputs but all result in the cubes instantiating well above or below the plan, or with a wonky rotation. I am looking for the cubes to be just above and parallel to the plane. Any help with this problem would be greatly appreciated!

 public class RestitutionCheck : MonoBehaviour {
 
  public PhysicMaterial[] slopeMaterials;
  public GameObject talusRocks;
  private Rigidbody thisRock;
  private Vector3 thisRockPosition;
  private Collider slopeObject;
  private Collider rockCollider;
  private bool talusOff;
  
  // Use this for initialization
  void Start () {
      thisRock = this.GetComponent<Rigidbody>();
      rockCollider = thisRock.GetComponent<Collider>();
      thisRockPosition = rockCollider.transform.position;
      talusOff = false;
  }
  
  // Update is called once per frame
  void Update () {
      Ray ray = new Ray(thisRock.transform.position, Vector3.down);
      Debug.DrawRay(thisRock.transform.position, Vector3.down, Color.green);
      RaycastHit hit;
      if (Physics.Raycast(ray, out hit, 1.5f, 1<<10))
      {
          string slopeTag = hit.collider.tag;
          slopeObject = hit.collider;
          int slopeInt;
          if (!int.TryParse(slopeTag, out slopeInt))
          {
              Debug.LogError("Slope tag parse failed");
          }
          switch (slopeInt)
          {
              case 0:
                  Console.WriteLine("slopeTag =" + slopeInt);
                  rockCollider.material = slopeMaterials[0];
                  if (!talusOff)
                  {
                      Instantiate(talusRocks, hit.point + (hit.normal * 0.4f), Quaternion.LookRotation(hit.normal));
                      talusOff = true;
                  }
                  break;
              case 1:
                  Debug.Log("slopeTag =" + slopeInt);
                  rockCollider.material = slopeMaterials[1];
                  break;
          }
      }
      Debug.Log("no hit detected.... no change to slope material");
  }
 }

alt text

talus-blocks.png (4.9 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast not finding collider when instantiated from code 1 Answer

My Raycast on awake fires and will not change Help! 0 Answers

Instantiate, null reference and Raycasting problem. 2D 1 Answer

In terms of performance, which one is faster, 2D raycast, collission or distance sorting? 0 Answers

Getting raycast to fire at touch position but only when second finger is on screen 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