- Home /
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);
         }
 
 
 
     }
 
 
 
 }
Answer by xGasper · Aug 22, 2015 at 11:18 PM
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).
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                