user3795555
user3795555

Reputation: 7

Plane object position to Plane GameObject position

Plane object position to Plane GameObject position.

Basically I've created a plane based on three points. But, for testing reasons I need to know where the Plane is. So, I've got a Plane GameObject (that comes with a mesh, texture, and all the other things that come with it) in my Hierarchy. I want to move the Plane GameObject to the position and the normal to be that of the Plane object.

Here's some of the code:

plane1Go = GameObject.Find("Plane1");
Plane plane1 = new Plane(Point1,Point2,Point3);

plane1Go.gameObject.GetComponent<Mesh>().vertices= new Vector3[4]{Point1,Point2,Point3,Point4}; 

As you can see I use the three points, used to make the Plane object to move the Plane GameObject.

First thing, the Plane GameObject does not move and I don't know why.

Second and more importantly, I want to use the Plane object to actually be the point of reference for the Plane GameObject.

Upvotes: 1

Views: 324

Answers (2)

rutter
rutter

Reputation: 11452

I'll suggest a two-step approach:

  • Position a GameObject on the plane
  • Make that GameObject face in a direction equal to the plane's normal

I assume that you have a mesh attached to the GameObject, perhaps a big quadrilateral of some sort.

Plane plane = new Plane(point1, point2, point3);

//technically, any point on the plane will work
//for our purposes, I'll take the average of your three inputs
Vector3 center = (point1 + point2 + point3) / 3f;

//find the plane's transform
Transform planeTransform = GameObject.Find("Plane1").transform;

//position the plane
//then make it face toward its normal
planeTransform.position = center;
planeTransform.LookAt(planeTransform.position + plane.normal);

The second part of your question is less clear:

I want to use the Plane object to actually be the point of reference for the Plane GameObject.

You could reposition planeTransform as often as needed. You could reposition it each time the plane changes, or once per frame in an Update function.

I do recommend caching the results of GameObject.Find if you're going to be calling this a lot.

Upvotes: 1

Zac Howard-Smith
Zac Howard-Smith

Reputation: 94

Unless it has changed, you should be modifying the Transform element of the gameobject.

GameObject.FindWithTag("PlaneTop").transform.position = new Vector3(10, 0, 0);

This maintains the mesh, but moves the object.

The reference point for the GameObject itself depends on where the local space origin of the mesh is placed when the mesh is exported. I'm not entirely sure what you are trying to achieve but it may be a case of repositioning the mesh itself, rather than the game object.

Upvotes: 1

Related Questions