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 NovaNoah · Mar 19, 2014 at 11:34 PM · c#instantiatetransformcubelocal

(C#) How does one position instantiated objects locally, not to the world?

So I'm making 2.5D game which has cubes fragment dynamically with the following script. It breaks a cube of any dimensions into 4 divisions on collision (cubes are only fragmented on the 2D plane, so Z always stays the same.) It works great except when a cube lands while rotated any amount. Here is a screenshot of what happens: (Link)

The cube fragments nicely, but positions the fragments according to the world and not the original gameObject (with variable name "original".) The new fragments are resized clones of the original, so after creating the fragment, I remove the destruction script from them.

Here is the full code, cleaned up a tiny bit for your convenience. (C#)

     using UnityEngine;
     using System.Collections;
     
     public class Destruction : MonoBehaviour {
     
         public GameObject original;
         public GameObject quad1;
         public GameObject quad2;
         public GameObject quad3;
         public GameObject quad4;
     
         public Vector3 oDim;
         public Quaternion oRot;
     // Start
         void Start () {
     
             original = gameObject;
     
             oDim = new Vector3(gameObject.transform.localScale.x,gameObject.transform.localScale.y,gameObject.transform.localScale.z);
     
             oRot = Quaternion.Euler(new Vector3(gameObject.transform.rotation.x,gameObject.transform.rotation.y,gameObject.transform.rotation.z));
     
     
         }
     // On collision.
         void OnCollisionEnter (Collision c){
     
             if (c.relativeVelocity.magnitude >= 10) {
     
                 Debug.Log ("Object has been smashed.");
     
                 Vector3 vect1 = new Vector3 (oDim.x/4, oDim.y/4, 0);
                 Vector3 vect2 = new Vector3 (oDim.x/-4, oDim.y/4, 0);
                 Vector3 vect3 = new Vector3 (oDim.x/4, oDim.y/-4, 0);
                 Vector3 vect4 = new Vector3 (oDim.x/-4, oDim.y/-4, 0);
                             
     
     // This is where I need help. The 4 fragments will inherit rotation properly, but position themselves according to the world, not according to the original object.
     
     // Instantiates gameObject called quad1 with the properties of the original. Inherits original's location and rotation, but offset position by "vect1. (See above.)
     
                 quad1 = GameObject.Instantiate (original, gameObject.transform.position + vect1, oRot) as GameObject;
                 quad1.transform.localScale = new Vector3(oDim.x/2,oDim.y/2,oDim.z);
                 quad1.rigidbody.velocity = gameObject.rigidbody.velocity;
                 Destroy(quad1.GetComponent("Destruction"));
     
                 quad2 = GameObject.Instantiate (original, gameObject.transform.position + vect2, oRot) as GameObject; // Repeats for the other fragments. Note: different offsets.
                 quad2.transform.localScale = new Vector3(oDim.x/2,oDim.y/2,oDim.z);
                 quad2.rigidbody.velocity = gameObject.rigidbody.velocity;
                 Destroy(quad2.GetComponent("Destruction"));
     
                 quad3 = GameObject.Instantiate (original, gameObject.transform.position + vect3, oRot) as GameObject;
                 quad3.transform.localScale = new Vector3(oDim.x/2,oDim.y/2,oDim.z);
                 quad3.rigidbody.velocity = gameObject.rigidbody.velocity;
                 Destroy(quad3.GetComponent("Destruction"));
     
                 quad4 = GameObject.Instantiate (original, gameObject.transform.position + vect4, oRot) as GameObject;
                 quad4.transform.localScale = new Vector3(oDim.x/2,oDim.y/2,oDim.z);
                 quad4.rigidbody.velocity = gameObject.rigidbody.velocity;
                 Destroy(quad4.GetComponent("Destruction"));
     
                 Destroy(gameObject);
                     }
             
         }
     }
     


Comment
Add comment · Show 7
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 supernat · Mar 19, 2014 at 11:38 PM 0
Share

For clarification, what is this script attached to? Because when you call Instantiate, you are passing the object type to instantiate and passing the transform.position of whatever this script is on, so if this script is on an empty game object at the world's origin, it will use that origin. Perhaps you meant original.transform.position for the second parameter to Instantiate?

avatar image NovaNoah · Mar 19, 2014 at 11:47 PM 0
Share

This script is attached to the intended smashable rigidbody cube in the game world. Wouldn't gameObject be functionally the same as original? I even equated the two (original = gameObject;) on line 17.

avatar image NovaNoah · Mar 19, 2014 at 11:49 PM 0
Share

Although, it just occurred to me that I stated they were equal in the void Start();, not Update.

I'll try what you said and get back to you.

Edit: No, it acts exactly the same as having gameObject.transform.position.

avatar image paulygons · Mar 20, 2014 at 12:34 AM 0
Share

$$anonymous$$aybe using gameObject.transform.localPosition + vect1 in your instantiate lines? I don't think I understand the question. Your image link shows 4 cubes offset from a center location. Are they all supposed to be stacked together in the center?

avatar image NovaNoah · Mar 20, 2014 at 12:55 AM 0
Share

That photo is taken the very instant of a large impact from 1 larger cube. The cube landed on the same angle that all the other cubes are rotated to.

The large cube then deletes itself, replacing itself with 4 smaller cubes, as if it smashed on impact. Unfortunately what's happening is that the 4 cube fragments are offsetting themselves along the world, not relative to the local object.

gameObject.transform.localPosition appears to do the same thing as the other alternatives I've tried.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by scipiothegreat · Mar 20, 2014 at 12:50 AM

Use transform.TransformPoint(position) to turn local space coordinates into world space.

Comment
Add comment · Show 3 · 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 NovaNoah · Mar 20, 2014 at 02:45 AM 0
Share

Yes!Thank you! This definitely seems like the right direction. I can't figure out how to implement this though. I tried this:

 vect1 = transform.TransformPoint(oDim.x/4, oDim.y/4, 0);
 vect2 = transform.TransformPoint(-oDim.x/4, oDim.y/4, 0);
 vect3 = transform.TransformPoint(oDim.x/4, -oDim.y/4, 0);
 vect4 = transform.TransformPoint(-oDim.x/4, -oDim.y/4, 0);

and put the instantiation position as simply the corresponding vect.

No luck. It puts the fragments in the right general area, but there are large spaces in between the fragments.

Am I going about this totally wrong?

avatar image scipiothegreat · Mar 20, 2014 at 03:16 AM 0
Share

Try using transform.TransformDirection. I've gotten weird behavior from these before.

avatar image NovaNoah · Mar 20, 2014 at 04:08 AM 0
Share

That did the trick! Thank you so much!

I used:

 vect1 = transform.TransformDirection(oDim.x/4, oDim.y/4, 0);
 vect2 = transform.TransformDirection(-oDim.x/4, oDim.y/4, 0);
 vect3 = transform.TransformDirection(oDim.x/4, -oDim.y/4, 0);
 vect4 = transform.TransformDirection(-oDim.x/4, -oDim.y/4, 0);

with an instantiation position of:

 vect + gameObject.transform.position

Completely working now. Again. Thank you.

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

22 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

Related Questions

C# Creating a More Efficient Script 4 Answers

Rotating a certain axis offsets the other ones? 1 Answer

How to instantiate prefab over the text one letter at a time? OR how to get transform of letters in a text 0 Answers

Random Object spawning with transforms 2D 1 Answer

Object instantiate, Projectile problems 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