Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Instantiated prefab clone as a target

Discussion in 'Scripting' started by Achim, Nov 19, 2018.

  1. Achim

    Achim

    Joined:
    Dec 19, 2007
    Posts:
    199
    Hallo,
    I want to make a boat go to an instantiated prefab clone (a buoy) when I click the mouse button. Then the buoy gets destroyed when the boat hits the prefab. If I then click on the water a new prefab gets instantiated and the boat should got to this new prefab, etc.

    This is the test scene:
    http://achimkinzelmann.homepage.t-online.de/Webseite/WebGL/Script Test 001 alt/index.html

    What I have so far is the script to instantiate the prefab clone with a mouse click
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BojeAbsetzen : MonoBehaviour
    5. {
    6.     public GameObject Boje;
    7.  
    8.     void Update()
    9.     {
    10.         if (Input.GetButtonDown("Fire2"))
    11.         {
    12.             Debug.Log("Mouse is down");
    13.  
    14.             RaycastHit hitInfo = new RaycastHit();
    15.             bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
    16.             if (hitInfo.transform.gameObject.tag == "Wasser")
    17.  
    18.                 //if (hit)
    19.             {
    20.                 Debug.Log("Wasser gefunden");
    21.                 GameObject BojeInstance;
    22.                 BojeInstance = Instantiate(Boje, hitInfo.point, Quaternion.identity) as GameObject;
    23.          
    24.  
    25.  
    26.             }
    27.         }
    28.     }
    29. }
    30.  
    , and a script for the boat to head to a target.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bswimtopickedtarget : MonoBehaviour
    5. {
    6.  
    7.     public Transform target1;
    8.  
    9.     public float damping1;
    10.  
    11.  
    12.  
    13.     void Update()
    14.     {
    15.  
    16.  
    17.         var rotation = Quaternion.LookRotation(target1.position - transform.position);
    18.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping1);
    19.         transform.rotation = Quaternion.Euler(new Vector3(0f, transform.eulerAngles.y, 0f));
    20.  
    21.  
    22.     }
    23.  
    24. }
    There are some other scripts for the movement of the boat, but these two are the ones who depend on the prefab clone target.
    How can I get the prefab clones as an updatet target ?
     
    Last edited: Nov 20, 2018
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    7,565
    Can you not just move BojeInstance to new location, instead destroying / creating ?
     
  3. Achim

    Achim

    Joined:
    Dec 19, 2007
    Posts:
    199
    I was thinking about this solution too, but I would like to know how to do it this way, because it is quite useful for many purposes.
     
  4. Achim

    Achim

    Joined:
    Dec 19, 2007
    Posts:
    199
    When I try it with
    Code (CSharp):
    1. target1 = GameObject.FindWithTag("Boje");
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bswimtopickedtarget : MonoBehaviour
    5. {
    6.  
    7.     public GameObject target1;
    8.    
    9.     public float damping1;
    10.    
    11.  
    12.  
    13.     void Update()
    14.     {
    15.  
    16.         target1 = GameObject.FindWithTag("Boje");
    17.  
    18.         var rotation = Quaternion.LookRotation(target1.position - transform.position);
    19.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping1);
    20.         transform.rotation = Quaternion.Euler(new Vector3(0f, transform.eulerAngles.y, 0f));
    21.        
    22.  
    23.     }
    24.  
    25. }
    then I get this error

    "Assets/Scripte/Bswimtopickedtarget.cs(17,56): error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `position' and no extension method `position' of type `UnityEngine.GameObject' could be found. Are you missing an assembly reference?"
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    7,565
    Is likely that,
    target1 should be Transform, not GameObject,
    or replace
    target1.position
    with
    target1.transform.position
     
    Achim likes this.
  6. Achim

    Achim

    Joined:
    Dec 19, 2007
    Posts:
    199
    Great, this works. Thanks. I googled a lot, but it was quite simple at the end.
    http://achimkinzelmann.homepage.t-online.de/Webseite/WebGL/Script Test 001/index.html
    Now I have to figure out how to reset the script to work every time I set a new buoy.
    And as a long time goal I´ll try to figure out how to get the hittarget from the raycast from the main camera.
    Thanks again.

    UPDATE:
    ok, got it working like it should:
    http://achimkinzelmann.homepage.t-online.de/Webseite/WebGL/Script Test 001 funktioniert/index.html
     
    Last edited: Nov 20, 2018
unityunity