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 AnimaCrucio · Jul 30, 2018 at 02:25 AM · gameobjectinstantiatebuttoncamera-movement

Bearded Multi-Headed Instantiation Dragoon Question

If I wanted to: (sorry for the complicated question)

  1. instantiate a sphere prefab with a name, a random position, and a random size

  2. instantiate a button for it on a scroll view

  3. on click of set button, make my only camera look at the sphere prefab I just instantiated

  4. while also on click, travel to the instantiated sphere prefab

  5. and end up at the same transform of the previous object,relative to the new one

I have conquered instantiating the sphere prefab with its random specifications. I can currently add a button prefab with the same sphere prefab name to an existing scroll view by the use of a list, a sphere prefab class, and a button prefab class. I would like to add a generic function to satisfy requirement 3, 4, and 5 in my button prefab onClick so it is linked to the new sphere prefab I have just instantiated. To make this all the more complicated I am instantiating 1000 of these sphere prefabs in a 3D environment at run time.

 public GameObject mainCamera;
 public float transitionSpeed;
 public float startTime = 1.0f;
 
 //the newSphere is the instantiated sphere prefab
 public void SphereButtonClick(GameObject newSphere)
     {
         Transform newSphereTrans = newSphere.transform;
         Vector3 sphereToZoomToPos = newSphereTrans.position;
         float sphereToZoomToX = sphereToZoomToPos.x;
         float sphereToZoomToY = sphereToZoomToPos.y;
         float sphereoZoomToZ = sphereToZoomToPos.z;
 
         Transform mainCameraTrans = mainCamera.transform;
         mainCameraTrans.SetParent(newSphereTrans);
         mainCameraTrans.LookAt(newSphereTrans);
         float lerpXValue = Mathf.Lerp(0, sphereToZoomToX, (Time.time - startTime) * transitionSpeed);
         float lerpYValue = Mathf.Lerp(0, sphereToZoomToY, (Time.time - startTime) * transitionSpeed);
         float lerpZValue = Mathf.Lerp(0, sphereToZoomToZ, (Time.time - startTime) * transitionSpeed);
         mainCameraTrans.position = new Vector3(lerpXValue, lerpYValue, lerpZValue);
     }

This is my camera movement function but I can't get it to work. I have attempted to slide it into the onClick of the button prefab as both a separate class and as a part of another but when I bring up the class the function does not exist. Is my function for onClick not allowed to take in a GameObject? If not, how would I take my instantiated sphere prefab game object (which has its own sphere prefab class) (and is also in a list that displays on the aforementioned scroll view) and link it to the main camera through an also instantiated button onClick?

My Sphere Prefab Class:

 public class SpherePrefab : MonoBehaviour
 {
     public GameObject sphereInScene;
     public Text SphereName;
 }

And my Button prefab Class:

 public class ButtonPrefab : MonoBehaviour {
 
     public Button button;
     public Text sphereName;
 
 }


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 Sonky108 · Jul 30, 2018 at 07:01 AM

First of all we can start with SphereFactory (singleton): public void SphereFactory: MonoBehaviour { public event Action SphereCreatedListeners;

     public void CreateNew()
     {
         //Create Vector3 random position
         //Create Vector3 random size
         var newObject = Instantiate(prefab, position);
         newObject.transform.localScale = randomScale;
         
         SphereCreatedListeners?.Invoke(newObject); //Assuming you are using C# 6.0
     }
 }

Next, we can create ButtonFactory (singleton):

 public void ButtonFactory: MonoBehaviour
 {
     public event Action<GameObject> SphereClickedListeners;
     
     void Start()
     {
         SphereFactory.Instance.SphereCreatedListeners += OnSphereCreated;
     }
     
     private void OnSphereCreated(GameObject sphere)
     {
         var newButton = Instantiate(prefab, gridLayout);
         newButton.onClick.AddListener(() => SphereAction(sphere));
     }
     
     private void SphereAction(GameObject sphere)
     {
         SphereClickedListeners?.Invoke(sphere); //C# 6.0
     }
     
     void OnDestroy()
     {
         SphereFactory.Instance.SphereCreatedListeners -= OnSphereCreated;
     }
 }
 

Finally CameraController basend on your code:

 public void CameraController: MonoBehaviour
 {
     private Transform target;
     
     void Start()
     {
         ButtonFactory.Instance.SphereClickedListeners += OnSphereClicked;
     }
     
     private void OnSphereClicked(GameObject sphere)
     {
         target = sphere.transform;
     }
     
     private void NavigateTo(GameObject target)
     {
         Vector3 sphereToZoomToPos = target.position;
         float sphereToZoomToX = sphereToZoomToPos.x;
         float sphereToZoomToY = sphereToZoomToPos.y;
         float sphereoZoomToZ = sphereToZoomToPos.z;
 
         Transform mainCameraTrans = mainCamera.transform;
         mainCameraTrans.SetParent(newSphereTrans);
         mainCameraTrans.LookAt(newSphereTrans);
         float lerpXValue = Mathf.Lerp(0, sphereToZoomToX, (Time.time - startTime) * transitionSpeed * time.deltaTime);
         float lerpYValue = Mathf.Lerp(0, sphereToZoomToY, (Time.time - startTime) * transitionSpeed * time.deltaTime);
         float lerpZValue = Mathf.Lerp(0, sphereToZoomToZ, (Time.time - startTime) * transitionSpeed * time.deltaTime);
         mainCameraTrans.position = new Vector3(lerpXValue, lerpYValue, lerpZValue);
     }
     
     void Update()
     {
         if(target == null) return;
         
         NavigateTo(target);
     
         if(HasArrived())
         {
             target = null;
         }
     }
     
     private bool HasArrived()
     {
         return target != null && (target.position - mainCamera.position).magnitude <= 0.1f;
     }
     
     void OnDestroy()
     {
         ButtonFactory.Instance.SphereClickedListeners -= OnSphereClicked;
     }
 }

I have missed some fields, methods and basic singleton pattern, but I hope you get the point :) I don't like idea of having sphere action in button factory (it should be defined in sphere script, like OnButtonClickAction).

Comment
Add comment · Show 2 · 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
avatar image AnimaCrucio · Aug 05, 2018 at 12:56 AM 0
Share

Thanks for taking the time. I will try and adapt the code you presented to my purposes and get back to you. A lot of good information there. Thanks again!

avatar image AnimaCrucio · Aug 05, 2018 at 01:04 AM 0
Share

I hope you don't $$anonymous$$d if I ask a few questions about your answer; this is completely new to me. What exactly do these lines do or mean?

SphereCreatedListeners?.Invoke(newObject); SphereClickedListeners?.Invoke(sphere);

I am fairly sure I am working with C# in Visual Studio 2017.

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

161 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet Prefab not instantiating 1 Answer

[SOLVED] Only instantiating once 1 Answer

Buttons not showing up the first time the menu loads, but showing up on all menu openings after 2 Answers

Button then instanitates gameobject 1 Answer

Instantiate object with button and place another object with mouse click 2 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