# Plane Struct point calculation.

I’m creating a homeworld style movement system and having a ton of difficulty getting unity to create a plane struct at the proper level of worldspace.

I want to create an XZ plane that is always flat (same as XZ in world) but want it to stay at the Y elevation of the object that I’m creating to calculate distance and direction from.

Basically, I want to create a struct plane to draw a disc/circle on and to use to intersect with a screen raycast to obtain a direction and distance from the ship (origin).

Here’s my existing code layout.

using UnityEngine;
using System.Collections;

public class UnitMovement : MonoBehaviour {

static GameObject objectNameInfo;
Vector3 inPoint;

void start()
{
objectNameInfo = RTSControlSelect.objectName;
inPoint = objectNameInfo.transform.position;
}

void update()
{
if (Input.GetKeyDown(KeyCode.M))
{

Plane.SetNormalAndPosition (vector3.up, vector3.inPoint);
}

rotateToPoint();

moveToPoint();

}

void rotateToPoint()
{

}

void moveToPoint()
{

}

}

I know I need to convert the transform.position that I’m passing into a vector3 or something in order to do this. How can I just get the Y value from the transform.position and pass it to the Plane.SetNormalAndPosition?

Second, how to I generate the struct in the first place? How do I give it a name or get data from it?

Lastly, how do I get the point of intersection with the raycast I plan to do or how do I store the distance and direction to the point of intersection?

On a side note, I plan to add a “leftshift” key modifier that moves the point of intersection using translate(vector3.up) with mouse Y input. I also want to allow this script to have a selected ship get the distance and direction to an object that is right clicked.

There is a lot wrong with this script. For starters, you need all your function names start with capitals (explicitly the ones which are required for unity callbacks- Start, Update, but you should do so with the others, too).

If you want to create a plane, do so just like any other object:

Plane shipPlane = new Plane(Vector3.up, inPoint);

When you create a plane, you give it a normal, and a point. These two pieces of information provide the direction that it is facing, and one position in space that it intersects- all the rest can be calculated from that.

Note the capital ‘V’ in Vector3. Then you can access it at any point within the same scope by using ‘shipPlane’. Transform.position is already a Vector3- there’s no need to do any kind of conversion there!

If you want to get the point of a ray, use this:

float position;
// Do this any way, but this is probably the most useful
Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);

if(shipPlane.Raycast(inputRay, out position))
{
// get the point in space!
Vector3 hitPoint = inputRay.GetPoint(position);

float distanceFromShip = Vector3.Distance(inPoint, hitPoint);
// do whatever you need to do with this!
}

This should get you started. Look up some C# examples- on stackOverflow or something. Get your head around the basic syntax. Glad you’re not using JS, btw- I can’t stand it either, and C# is much more useful for you outside of Unity development.