Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate object with rotation from transform hit by ray.

Discussion in 'Scripting' started by Slaghton, Feb 1, 2015.

  1. Slaghton

    Slaghton

    Joined:
    Sep 9, 2013
    Posts:
    139
    Hi, i've been having trouble with a script and after doing some searches and being a bit new to scripting still I thought i'd just ask for anyones guidance :x

    Alright, so I got a script that instantiates an object at a ray.hit position but what i'm trying to add on to it is getting the rotation of the transform hit by the ray, storing it in a variable, then using it in the instantiate prefab line to replace the Quaternion.identity rotation.

    I'm not sure the best way to do it or what ways are/aren't possible since im fairly new. I'll be around for another 5-6 hours before i grab breakfast and pass out from being up all nite lol.

    Edit: Just stumbled upon this line of code. It's definitely changing the rotation based on the object its hitting with my tag but doesn't seem its matching the objects rotation. Hmm

    var rotationOfHitObject :Quaternion= hit.collider.gameObject.transform.rotation;

    EDIT 2: Ok this code is working. I shouldn't of been testing a script that rotates a prefab having it shaped similar to a cube with targeting everything with a rotation at 90 degree angles.

    Edit 3: Ok I guess what i have to do now and spawn the object at the transform thats been hit instead of spawn_position = Vector3(hit.point.x, hit.point.y, hit.point.z);

    Edit 4: I got the transform.position too now lol. Going to see if i can get everything working together.

    Finished: Alright script complete. I now have the base foundation for my building construction script working and online camptible :D.
     
    Last edited: Feb 1, 2015