• 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
1
Question by Panamamigo · Jun 28, 2010 at 04:04 AM · parentprecisionmagnet

Parenting at runtime with precision, help!

Hello,

I am having trouble getting one object parented to the right point. Basically I have two spheres, a blue one and a red one. The blue one has two small yellow cylinders on the either side of it (sort of like arms or airplane wings). The red sphere has one cylinder on it's front (like a nose). I am in control of the blue sphere and shoot little cyan squares at the red sphere. When I shoot it five times (it's health is at 0) it is no longer rendered and all that is visible is the halo effect. The gravity and detect collisions is also turned off at this point. Then the red sphere looks at me (the blue sphere) and translates forward. Then when it is close enough it snaps into the same position as an empty that is at the end of one of the cylinders and becomes parented to the blue sphere. In addition it has it's render restored and it appears solid again. The problem is that it does not parent so that the cylinders of each sphere match up forming a kind of link or bond together. Any ideas as to how I could solve this? I want the red sphere to eventually wander around with some AI but when shot 5 times will fly over and attach or connect to the blue sphere and then have them become a single unity. In addition it would be helpful if I could recalculate the center of the two combined objects so that the pair will rotate around the center of the two spheres. Here are some pictures.

starting up

Shooting

Coming at me

This is how it connects faulty

A sloppy version of how it should look

//This script finds the empties position static var MPosition : Vector3; static var ActiveMagnet : GameObject;

function Update () {

 ActiveMagnet=gameObject;
 MPosition=ActiveMagnet.transform.position;

}

 //This finds the red sphere (enemy)'s position

static var EPosition : Vector3;

function Update () {

 EPosition=gameObject.transform.position;

}

 //This finds the Distance between the blue and red spheres

static var TotalDistance : float;

function Update () {

 var EP=EnemyPosition.EPosition;

 var MP=MagnetPosition.MPosition;

 var DistanceApart=MP-EP;

 var DAX=Mathf.Abs(DistanceApart[0]);

 var DAY=Mathf.Abs(DistanceApart[1]);

 var DAZ=Mathf.Abs(DistanceApart[2]);

 TotalDistance=DAX + DAY + DAZ;

}

 //This is the script that handles the enemy's health and the magnetism and stuff

var Magnet : Transform;

var sprites= new Array();

var MagnetForce= 0.1;

var Enemy : Transform;

private var On : boolean= false;

private var EHealth=50;

function OnCollisionEnter (collision : Collision) {

 if(collision.gameObject.tag==("Player Bullet") && EHealth > 0){

     EHealth= EHealth - 10;

     Debug.Log(EHealth);

 }


 if(EHealth==0){

     gameObject.layer= 8;

     gameObject.rigidbody.detectCollisions = false;

     gameObject.rigidbody.useGravity = false;

     renderer.enabled= false;
 }


 if(renderer.enabled != true){
     sprites.Add(gameObject.name);
     On= true;
 }

}

function Update(){

 if(On != false){
     gameObject.transform.LookAt(Magnet);
     gameObject.transform.Translate(Vector3.forward * MagnetForce, Space.Self);
 }

 if(On != false && DistanceApart.TotalDistance <= 1.5){
     gameObject.rigidbody.MovePosition(Magnet.position);
     Enemy.parent= Magnet;
     On = false;
     EHealth=EHealth + 10;
     renderer.enabled= true;
 }

}

I'm sorry this is such a lengthy problem, but I greatly appreciate any help I can get. I would be more than happy to upload the source if someone alerts me to some method of doing so. Again thank you for all the help, the Unity community really is one of the best tools this engine has to offer! I might also run into a problem with having multiple enemies doing the same thing. I'll probably use the array, sprites, later on once I get this problem solved.

Comment
Add comment · Show 2
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 qJake · Jun 28, 2010 at 05:11 AM 0
Share

You need to use the Direct Link URL from Imageshack, not the page you get when you just upload it. Also, format your code by hilighting it and clicking the Code Format button next time.

avatar image Panamamigo · Jun 28, 2010 at 05:50 AM 0
Share

Okay SpikeX, thank you I will do that in the future. Do you have any ideas as to how I could solve my problem that this thread addresses?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ashkan_gc · Jun 28, 2010 at 05:57 AM

use transform.parent to make another object parent of this one. Then the children's movement and rotation and scale will be related to it's parent.

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 Panamamigo · Jun 28, 2010 at 06:31 AM 0
Share

Is this in reference to recalculating the new objects center? How could I get the cylinders to line up evenly?

avatar image Ashkan_gc · Jun 28, 2010 at 07:12 PM 0
Share

it's center is it's pivot point as before parenting but the center of the connection between two objects is another thing. you can calculate by deviding relative position of the child by 2.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Make a simple tree 1 Answer

sphere animation magnet to another object 0 Answers

I need to be able to get an object to attach to another one on mouse hold 0 Answers

Can a parent SetActive a child GameObject anymore 2 Answers

Moving a GameObject reffering to the coordinates of the parent Object 1 Answer


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