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 MrBalin · Mar 02, 2015 at 03:08 AM · rotationinstantiateraycasttransformposition

transform position and rotation of instantiated object

I'm having a dickens of a time with this code. While this code does what I want with a game object already in the scene at start, I don't know how to do it when I instantiate a prefab. Another goal is to destroy that clone on GetButtonUp("Fire1").

My goal is to have the clone follow the ray cast found hit exactly how blink pin would with a game object already in scene at start.

using UnityEngine; using System.Collections;

 public class Blink : MonoBehaviour {
     
     public GameObject blinkPin;
     public float maxDistance = 10f;
     
     public static Transform cam;
 
     Vector3 camPos;
     bool summonedPin = false;
     
     
     // Use this for initialization
     void Start () {
     
 
 
         cam = Camera.main.transform;
     }
     
     // Update is called once per frame
     void Update () {
     
 
         bool foundHit = false;
     
         RaycastHit hit = new RaycastHit ();
         // new RaycastHit ();
 
         //add camera local transform
         camPos = GameObject.FindWithTag ("Player").transform.Find ("Main Camera").transform.TransformPoint (0,0, maxDistance);
 
 
 
         if (Input.GetButton ("Fire1"))
         {
             foundHit = Physics.Raycast ((transform.position + new Vector3 (0,0,0)), cam.forward, out hit, maxDistance);
         }
 
         if (foundHit)
         {
             Vector3 posPin = hit.point;
             Quaternion rotPin = Quaternion.LookRotation (hit.normal);
 
 
             if (!summonedPin)
             {
 
                 GameObject newPin = Instantiate (blinkPin, posPin, rotPin) as GameObject;
 
                 //why does this part not work?
                 newPin.transform.position = hit.point;
                 newPin.transform.rotation = Quaternion.LookRotation (hit.normal);
                 summonedPin = true;
             }
 
             //if already in scene from start.
             //blinkPin.transform.position = hit.point;
             //blinkPin.transform.rotation = Quaternion.LookRotation (hit.normal);
         }
 
         if (!foundHit && Input.GetButton ("Fire1"))
         {
             //make sure to change child cube to 0 0 0 here, and back to original in first if
 
 
             blinkPin.transform.position = camPos;
             blinkPin.transform.rotation = transform.rotation * (Quaternion.Euler (-90, 0, 0));
 
             //spins master
             //master.Rotate (Vector3.up, Space.World);
         }
 
 
 
     }
 
 
 
 }
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 xGasper · Aug 22, 2015 at 11:18 PM

@MrBalin

Hi, i am sry i have never used rays before, but i have created a simple program that Instantiate's a prefab and than moves it around (it follows some other game object). I hope this will help u. Feel free to say if i forgot anything or made a mistake. (I hope i didn't do any mistake in the code)

 Transform spawnedPrefab;
 
 GameObject theObjectWeAreGoingToMove;
 public GameObject prefabToInstantiate;
 public GameObject objectToFollow;
 
   void Start () {
 
      handWepon = Instantiate (prefabToInstantiate, objectToFollow.transform.position, objectToFollow.transform.rotation) as Transform;
 // In the line above we instaniate the prefab at the position of the object we want to follow.
 
      theObjectWeAreGoingToMove= GameObject.Find (prefabToInstantiate(Clone));
 //In the line above we find the instantiated prefab in the actual game. 
 //Till now u tried to move instantiated prefab that only existed in source code.
 //To find the clone that appeared in the game u have to know its exact name!!!
 }

   void Update () {
       theObjectWeAreGoingToMove.transform.position = objectToFollow.transform.position;
       theObjectWeAreGoingToMove.transform.rotation= objectToFollow.transform.rotation;
  // In update we only need to move the object that we faund before.
  }

I think the part with finding the object in the actual game will save your problems (btw the instanteated object in the game always has name of prefab u instanteated + (Clone) at the end so... instanteatedPrefab(Clone) for instance).

I think that when u find that Clone (Instaneated prefab) in the game u can also destroy it. (Tell me if i am wrong and I'll try to find how to destroy the Clone as well).

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to instantiate object at particular location 1 Answer

"Object reference not set to an instance of an Object" 0 Answers

How to make a gameobject rotation equal to ray transform? 0 Answers

Instantiate Bullet Hole position and rotation, Vector3 issues 1 Answer

Help with moving instantiated object's transform 1 Answer


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