- Home /
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.
PLEASE HELP THIS POOR FRUSTRATED SOUL!!! BTW, I don't code in JavaScript. All C#.
Okay so this all works...
But I want to draw a line from inPoint to hitPoint.
I've tried to pass the data of objectNameInfo.transform.position ("= RTSControlSelect.objectName") to inPoint. However, no matter how I seem to try and pass the data, it always gives me a null reference.
I have an RTSCameraControl script that inherits the same data from RTSControlSelect in the same way... Why won't it work here?
I don't really understand your question. Can you tell me the exact line of code? I'm not sure what you're trying to do, but if I see the code I might be able to work it out.
Answer by syclamoth · Mar 22, 2012 at 01:00 PM
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.
TY!!
also why do I have to use capitals? Non of my other C# scripts have them in those places and they work perfectly! I'm not questioning you, I'd just like to know the reasoning behind it.
Also thanks very much, I was thinking along the same lines but wasn't sure how to write it out exactly.
They work, but it's just best practice to use Capitals for typenames and methods, and lower case (or camelCase) for variables and members. The point of this is to make it easier to read- the compiler doesn't really care.