Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by thundax · Mar 22, 2012 at 11:55 AM · movementraycastplanestructintersection

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#.

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 thundax · Mar 23, 2012 at 10:49 AM 0
Share

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?

avatar image syclamoth · Mar 24, 2012 at 04:29 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · 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 thundax · Mar 22, 2012 at 11:28 PM 0
Share

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.

avatar image syclamoth · Mar 24, 2012 at 12:08 AM 0
Share

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.

avatar image thundax · Mar 24, 2012 at 01:43 AM 0
Share

Gotcha.. TY

Did you see my comment on my question?

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Paddle script for Pong-based video game for Android 0 Answers

Stop the movement to a direction 0 Answers

Math - calculate position in world space from ray on infinite plane 2 Answers

Prevent random movement allowing movement through obstacles 1 Answer

Dashing through enemies, but not walls. 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