Jump to content

How to find the transform of a part


Recommended Posts

I am using empty gameobjects in unity to assign the attach nodes for my part. How do I get the transform object that I've assigned to the part, without returning the transform of a different part's transform.

My current method of getting the transform:


public override void OnStart(StartState state)
{
Debug.Log (GameObject.Find ("topAttach").transform.position);
}

Sure it works, but when there's multiple of these parts in play, I can't be sure I'm getting the right transform of the corresponding part...

On a side note, do you know why I'm getting two logs to console when I put the ship on the pad? Shouldn't it be called only once?

Edited by Ishkur
Link to comment
Share on other sites

I am using empty gameobjects in unity to assign the attach nodes for my part. How do I get the transform object that I've assigned to the part

If you're looking for a particular transform in the Part's model hierarchy, there's Part.FindModelTransform and Part.FindHeirarchyTransform. There's also findAttachNode(string), although I seem to recall having some trouble getting that one to work in the past. Part.transform.Find may also work.

Just in case you get thrown a curveball, something to check is whether the above will return transforms outside of the part's normal model hierarchy. For example, your part (near the top of the vessel tree) connected to a child octostrut. The octostrut doesn't have physics of its own* when attached to a parent that does, so it's possible that the above functions (minus perhaps findAttachNode) will also search the octostrut's model which is probably undesired. Using the part's prefab hierarchy instead should solve that if it does come up

*has no rigidbody nor joint and instead its transform becomes a child of the part's transform

Edited by xEvilReeperx
Clarity
Link to comment
Share on other sites

Thanks a lot, part.FindModelTransform did it for me!

For some reason, though, I had to use underscore part.FindModelTransform rather than your suggested uppercase Part.FindModelTransform (there wasn't any code completion for the uppercase spelling).

Any idea why that is? I would've found the method myself if I had looked through the completion for part. rather than Part.

Link to comment
Share on other sites

Thanks a lot, part.FindModelTransform did it for me!

For some reason, though, I had to use underscore part.FindModelTransform rather than your suggested uppercase Part.FindModelTransform (there wasn't any code completion for the uppercase spelling).

Any idea why that is? I would've found the method myself if I had looked through the completion for part. rather than Part.

No, you found the right one. I specify the class name when describing a function because whatever you're working with might not have a variable named "part" in scope (a simple MonoBehaviour working with a vessel, for example). Sorry if that caused any confusion. I'll try to be clearer in the future.

Link to comment
Share on other sites

I think that's more my inexperience with code, since I still don't understand what you said haha.

I don't have "part" defined anywhere in my class, but it still worked for some reason.


using System;
using UnityEngine;

namespace PartTesting
{
public class TestPart: PartModule
{
Transform axle;

public override void OnStart(StartState state)
{
axle = part.FindModelTransform("axle");
}

public override void OnUpdate()
{
this.transform.RotateAround(axle.position, Vector3.up, Time.deltaTime*20);
}
}
}

Link to comment
Share on other sites

  • 2 weeks later...

If you are using Visual Studio then you can put the cursor on the word PartModule and hit f12 (Go to definition) and it will show you the whole class declaration (not the actual guts of the code but the data and function signatures of all the class members). You can use f12 on any variable, function, class or type name and it will show you the definition in context, e.g. if you put the cursor on the "part" of "this.part" and hit f12 it will show you the PartModule class (as that is where part is defined) and it will highlight the "public Part part { get; set; }" line. So the part member variable is of the Part class. Doing f12 on "Part" shows you the definition of the Part class allowing you to find all the handy functions...

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...