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
1
Question by eXtremeUnity · Jul 14, 2014 at 05:11 AM · camerarotationdirectionfollowspace

Rotate spaceship based on mouse movement

Hello guys, First off, let me tell you I've really search A LOT, and I think I was always onto something, but never spot on..

So I have a spaceship in 3D space, similar to this pics bellow: (don't judge, it's paint) alt text

I want my spaceship to kinda be stationary, meaning it shouldn't really move much from that position, but I want to use my mouse to look around and ship's rotation to follow let's say that cursor in the center of the screen, or in other words - just to follow my camera's rotation..

So if I were to move my mouse right, I guess this should happen: alt text

Or if you ever happened to play StarConflict that's spot-on what I'm trying to achieve.. Here's YouTube link to a video featuring that game and look at 10:43..

On another note I'm using rigidbody, and so far my Main Camera is outside my ship object, it has some script that just keeps it away from ship and keep it being there.. I also managed to move my ship in the direction the camera is facing using this:

 transform.rotation = Quaternion.FromToRotation(transform.up, GameObject.FindGameObjectWithTag("MainCamera").transform.forward) * transform.rotation;;
 
 cacheRigidbody.velocity = GameObject.FindGameObjectWithTag("MainCamera").transform.forward * Input.GetAxis("Vertical") * 50;

Please help guys! :(

untitled.png (7.2 kB)
untitled2.png (4.9 kB)
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
3

Answer by robertbu · Jul 14, 2014 at 06:27 AM

Here is what I see in the video:

  • The camera follows ship. The ship does not follow the direction of the camera.

  • The camera has slerp/lerp rotations with limits that allow the ship to get ahead of the camera. This allows the camera to see part of the ship's turn.

  • A ship has pitch, yaw, and roll (reference here). The camera is following (with lag) the pitch, yaw, and roll. There appears to some control (not the mouse cursor) that rolls the ship without turning the ship. There is also some roll (banking) associated with yaw turns.

  • Turns occur when when the cursor is outside the middle circle...above the circle, and you get a rotation on the ship's local 'x' axis. To the side of the circle and ship rotates around the 'y' axis. For turns, the 'z' axis (banking) is driven by the 'y' rotation.

  • While I could not tell form the video, I'll bet the speed of the turn depends on the distance the cursor outside the center circle.

Here is something really simple to get you started. It uses as square. I cannot tell if the ship in the video uses a square or a circle. This code does not deal with roll (banking), but just yaw and pitch. I uses a fraction of the screen height as the calculation so that movements on different resolution devices will be consistent:

 #pragma strict
 
 var noTurn = 0.1; // Extent of the no-turn zone as a fraction of Screen.height;
 var factor = 150.0;
 private var center : Vector3;
 
  function Start() {
      center = new Vector3(Screen.width / 2, Screen.height / 2, 0);
  } 
  
 function Update () {
     var delta = (Input.mousePosition - center) / Screen.height;
     Debug.Log(delta);
     if (delta.y > noTurn) 
         transform.Rotate(-(delta.y - noTurn) * Time.deltaTime * factor, 0, 0);
     if (delta.y < -noTurn)
         transform.Rotate(-(delta.y + noTurn) * Time.deltaTime * factor, 0, 0);
     if (delta.x > noTurn)
         transform.Rotate(0, (delta.x - noTurn) * Time.deltaTime * factor, 0);
     if (delta.x < -noTurn)
         transform.Rotate(0, (delta.x + noTurn) * Time.deltaTime * factor, 0); 
 }

 
Comment
Add comment · Show 1 · 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 eXtremeUnity · Feb 01, 2015 at 12:56 AM 1
Share

Great answer! It's a bit strange that I'm answering this almost a year later, but my passion for this hasn't passed away. I'll be playing with this in following weeks and will write here my progress/additional questions..

Once again, thank you, will mark it as answer as soon as I get it in-editor. :)

avatar image
0

Answer by SyndicateXx · May 30, 2015 at 11:30 AM

Works all right! Really thanks bro... I still am trying to alter this script to tilt the ship to the direction the ship is moving in.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

rotate camera on mouse reaching edges 1 Answer

Make the player go into the direction I am looking at 0 Answers

Camera Shaking At High Speed 1 Answer

Adjust camera rotation while looking at a target 1 Answer

Relative Movement Problem 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