- Home /
(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);
}
}
}
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?
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.
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.
$$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?
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.
Answer by scipiothegreat · Mar 20, 2014 at 12:50 AM
Use transform.TransformPoint(position)
to turn local space coordinates into world space.
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?
Try using transform.TransformDirection
. I've gotten weird behavior from these before.
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
Follow this Question
Related Questions
C# Creating a More Efficient Script 4 Answers
Rotating a certain axis offsets the other ones? 1 Answer
Random Object spawning with transforms 2D 1 Answer
Object instantiate, Projectile problems 2 Answers