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 Katos · May 15, 2013 at 12:43 PM · movement3dmousespace

Move object towards mouse in full 3D space

Hello,

I'm working on a 3D space game and would like to move the ship like in EVE. This means, I can rotate the camera around the ship (got that) and double-click in a direction to turn and move there. The camera is always focused on the player ship, but can move further out and rotate around it.

1) How do I get the point where the mouse is? I was thinking about surrounding the player ship with a sphere and ray intersect it, but it seems like the sphere needs a bounding box to be intersected, which then pushes my ship (rigidbody) out (and would probably push away other objects aswell). Is there a way to intersect with it, but ignore it for physics? Or a totally different way of doing this?

2) How to properly rotate the ship towards that point, is there a semi-simple way to use rigidbody physics for that or is it better to skip physics and directly tween the rotation? Guess this is rather alot of code, so general advices would help already.

First question is most important for me to move on, if you can provide code samples I would prefer Javascript, but anything will do. I'll add further details if needed.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mexallon · May 15, 2013 at 12:49 PM

Hej there.

1) Have a look at the **Physics.Raycast** class. As I played EVE alot (see username :)) i know what you want. I think what you need for that in Unity is something like this .

2) Rotating the ship is really easy. You can use transform.LookAt to let the ship look into that direction you got from the raycast. What you probably want to add after that is done is a linear interpolation from the current rotation to the desired because the ship would just change its rotation in one frame. See this reference.

If you have no Idea of how to use Lerp (Linear Interpolation) functions have a look at this great tutorial.

Comment
Add comment · Show 2 · 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 Katos · May 15, 2013 at 01:18 PM 0
Share

1) Thanks, the problem was that your example needs something to intersect with, but ray.GetPoint(100.0) seems to do the trick without any intersect.

 var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var destination : Vector3 = ray.GetPoint (100.0);

It's not a true destination, but a point I can rotate towards.

2) LookAt already changes the object, is there a function to get the target rotation without actually rotating the object? And I'm still curious how it could work with proper physics, but you got me started :)

avatar image Mexallon · May 15, 2013 at 02:58 PM 0
Share

to 2): What do you mean with target rotation? You could calculate the angle between the current rotation and the desired - if thats what you want have a look at Vector.Angle.

avatar image
0

Answer by supamigit · Apr 25, 2014 at 12:17 PM

I set up 6 planes in every direction 2000 away from center then used this script... if moves from start which is next thing to change to a +- fast slower method...

Raycast bounces off the planes allowing the ship to move up and down in the Z access

oh left click double initiates move to that direction right click hold allows to move the camera 360 all directions around the ship

Code Below..........

 #pragma strict
 
 
 var PlayerCamera : Transform;
  
 private var ThisTransform : Transform;
  
  
  
 var CameraOffset : Vector3 = Vector3(0,3,10);
  
 var CameraSpeed : int = 200;
  
  
  
 var yMinLimit = -90;
  
 var yMaxLimit = 90;
  
  
  
 private var MouseInput : Vector2;
  
 private var directionTurn : Vector3;
  
 private var singleClick : boolean = false;
  
 private var count : int;
  
 private var Turn : boolean;
  
 private var cTimer : float;
  
 var doubleClickTime : float = .5;
  
  
  
 function Start () {
  
 ThisTransform = transform;
  
     PlayerCamera.eulerAngles = Vector3(0, 0, 0);
  
     PlayerCamera.position = Vector3(ThisTransform.position.x, ThisTransform.position.y + CameraOffset.y, ThisTransform.position.z - CameraOffset.z);
  
 }
  
  
  
 function Update () {
  
  
  
 ThisTransform.Translate(Vector3.forward * 5 * Time.deltaTime);
  
  
  
     if(Input.GetMouseButtonDown(0)){
  
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
         singleClick = true;
  
         count ++;
  
     }
  
  
  
     if(singleClick == true){
  
         cTimer += Time.deltaTime;
  
  
  
         if(count >1){
  
             directionTurn = ray.direction * 2000;
  
             Turn = true;
  
             singleClick = false;
  
             count = 0;
  
             cTimer = 0;
  
         }
  
  
  
         if(cTimer >= doubleClickTime){
  
             count = 0;
  
             singleClick = false;
  
             Turn = false;
  
             cTimer = 0;
  
         }
  
     }
  
  
  
         if(Turn){
  
             var Rotation = Quaternion.LookRotation(directionTurn - ThisTransform.position);
  
             ThisTransform.rotation = Quaternion.RotateTowards(ThisTransform.rotation, Rotation, Time.deltaTime * 50);
  
             ThisTransform.eulerAngles.z = 0;
  
  
  
             var targetDir = directionTurn - transform.position;
  
             var forward = transform.forward;
  
             var angle = Vector3.Angle(targetDir, forward);
  
         if(angle < 0.05){
  
             Turn = false;
  
         }
  
     }
  
 }
  
  
  
 function LateUpdate(){
  
 UpdateCamera();
  
 }
  
  
  
 function UpdateCamera () {
  
  
  
 var rot : Quaternion = Quaternion.Euler(MouseInput.y, MouseInput.x, 0);
  
 var pos : Vector3 = rot * Vector3(0.0, CameraOffset.y, -CameraOffset.z) + ThisTransform.position;
  
  
  
     if(Input.GetMouseButton(1)) {
  
         MouseInput.x += Input.GetAxis("Mouse X") * CameraSpeed * Time.deltaTime;
  
         MouseInput.y -= Input.GetAxis("Mouse Y") * CameraSpeed * Time.deltaTime;
  
         MouseInput.y = ClampAngle(MouseInput.y, yMinLimit, yMaxLimit);
  
     }
  
  
  
 PlayerCamera.position = pos;
  
 PlayerCamera.rotation = rot;
  
 }
  
  
  
 static function ClampAngle (angle : float, min : float, max : float) {
  
     if (angle < -360)
  
         angle += 360;
  
     if (angle > 360)
  
         angle -= 360;
  
     return Mathf.Clamp (angle, min, max);
  
 }
Comment
Add comment · 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

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Mouse Movement (Tracking) 1 Answer

Animation not working correctly on moving object 1 Answer

Attach an object to the mouse cursor? 0 Answers

Moving a gameobject with mouse 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