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 Po0ka · Oct 18, 2013 at 07:12 PM · cameragravityspaceviewlocking

Camera (view locking and movement) on a rigidboy

I am trying to make a space game in which a player can be sent in space, this player has a rigid body, so it can rotate on all axis. But with this came a problem about the view.

I must have a view relative to the player orientation, and this in a restricted view cone.

Lets say a view cone restriction of -60 and 60, you can be thrown in space, you would be rotating on the z axis, so would be your view. But what i want is the view to be realist. I made a planet that we can land on, but, if you land under the planet, you see upside down, and if you go on the sides, you see the planet surface on the side. -> This is what i want to fix.

(i already have the gravity code for the player orientation for the planet, but not the view)

 using UnityEngine;
 using System.Collections;
 
 public class GravitationMovement : MonoBehaviour
 {
     public Transform gravitySource;
     public float gravScalar = 4.0f;
     public Vector3 rotOffset = Vector3.zero;
 
     void FixedUpdate ()
     {
         var direction = gravitySource.position - transform.position;
         rigidbody.AddForce(direction.normalized * gravScalar);
         transform.rotation = Quaternion.LookRotation(direction.normalized) * Quaternion.Euler(rotOffset);
     }
 }

I now need a script to control how the view would turn relative to the parent.

Here is what i tried, and it failed hard, i think i need quaternions, but i couldn't find helpful references anywhere, even on the wiki.

 using UnityEngine;
 using System.Collections;
 
 public class CameraMovement : MonoBehaviour
 {
     bool lockView = false;
     
     float sensitivityPitch = 4F;
     float sensitivityYaw = 4F;
     float rotationPitch;
     float rotationYaw;
     float rotationRoll;
     
     float rotMinimumPitch = 0;
     float rotMaximumPitch = 0;
     
     float rotMinimumYaw = 0;
     float rotMaximumYaw = 0;
     
     void Update ()
     {
         rotationYaw += Input.GetAxis("Mouse X") * sensitivityYaw;
         rotationPitch += Input.GetAxis("Mouse Y") * sensitivityPitch;
         
         Mathf.Clamp(rotationYaw, rotMinimumYaw, rotMaximumYaw);
         
         rotMinimumPitch = transform.parent.eulerAngles.x - 10;
         rotMaximumPitch = transform.parent.eulerAngles.x + 10;
         
         rotMinimumYaw = transform.parent.eulerAngles.y - 10;
         rotMaximumYaw = transform.parent.eulerAngles.y + 10;
         
         print("minpitch = " + rotMinimumPitch);
         print("maxpitch = " + rotMaximumPitch);
         
         print("minyaw = " + rotMinimumYaw);
         print("maxyaw = " + rotMaximumYaw);
         
         if (rotationPitch > rotMinimumPitch)
         {
             if (rotationPitch < rotMaximumPitch)
             {
                 Vector3 rot = new Vector3(-rotationPitch, rotationYaw, 0);
                 transform.localEulerAngles = rot;
             }
             else
             {
                 transform.eulerAngles = new Vector3(rotMaximumPitch, transform.eulerAngles.y, transform.eulerAngles.z);
             }
         }
         else
         {
             transform.eulerAngles = new Vector3(rotMinimumPitch, transform.eulerAngles.y, transform.eulerAngles.z);
         }
         
         if (rotationYaw > rotMinimumYaw)
         {
             if (rotationYaw < rotMaximumYaw)
             {
                 Vector3 rot = new Vector3(-rotationPitch, rotationYaw, 0);
                 transform.localEulerAngles = rot;
             }
             else
             {
                 transform.eulerAngles = new Vector3(transform.eulerAngles.x, rotMaximumYaw, transform.eulerAngles.z);
             }
         }
         else
         {
             transform.eulerAngles = new Vector3(transform.eulerAngles.x, rotMinimumYaw, transform.eulerAngles.z);
         }
     }
 }
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
Best Answer

Answer by Po0ka · Oct 19, 2013 at 03:36 AM

Nevermind, i found it out!! I just had to use the localEulerAngles more, i tried it at first but it wasn't working right so i deleted the code for it and went with the hard way xD

So, here's the code if anybody tried searching similar things.

 using UnityEngine;
 using System.Collections;
 
 public class CameraMovement : MonoBehaviour
 {
     bool lockView = false;
     
     float sensitivityPitch = 4F;
     float sensitivityYaw = 4F;
     public float rotationPitch;
     public float rotationYaw;
     
     float rotMinimumPitch = 0;
     float rotMaximumPitch = 0;
     
     float rotMinimumYaw = 0;
     float rotMaximumYaw = 0;
     
     void Update ()
     {
         rotationYaw += Input.GetAxis("Mouse X") * sensitivityYaw;
         rotationPitch += Input.GetAxis("Mouse Y") * sensitivityPitch;
         
         rotMinimumPitch = -10;
         rotMaximumPitch = 10;
         
         rotMinimumYaw = -10;
         rotMaximumYaw = 10;
         
         if (rotationPitch < rotMinimumPitch)
         {
             rotationPitch = rotMinimumPitch;
             transform.localEulerAngles = new Vector3(rotMinimumPitch, transform.localEulerAngles.y, transform.localEulerAngles.z);
         }
         
         if (rotationPitch > rotMaximumPitch)
         {
             rotationPitch = rotMaximumPitch;
             transform.eulerAngles = new Vector3(rotMaximumPitch, transform.localEulerAngles.y, transform.localEulerAngles.z);
         }
         
         if (rotationYaw < rotMinimumYaw)
         {
             rotationYaw = rotMinimumYaw;
             transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, rotMinimumYaw, transform.localEulerAngles.z);
         }
         
         if (rotationYaw > rotMaximumYaw)
         {
             rotationYaw = rotMaximumYaw;
             transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, rotMaximumYaw, transform.localEulerAngles.z);
         }
         
         Vector3 rot = new Vector3(-rotationPitch, rotationYaw, 0);
         transform.localEulerAngles = rot;
     }
 }

Also, a note to the people like me who duplicate the same codes in 2 different if statements; put it one time after both ifs, that's win.

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
avatar image
0

Answer by zombience · Oct 18, 2013 at 08:45 PM

so, unfortunately 3D math and quaternion madness is my weak point... however, this should at least handle the orientation so that the camera's "forward" is always perpendicular to the surface of the planet. My answer does not take into account user rotations or distance from the surface.

I don't think this will completely solve your problem, but perhaps give you a starting point on how to ensure that the "planet" is always "down" to your camera.

anyhoo, one line of code for rotations (again, this doesn't handle user look rotation or distance from surface)

 transform.rotation = Quaternion.LookRotation(Vector3.Cross(transform.position - center.position, center.position), transform.position - center.position);
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 Po0ka · Oct 19, 2013 at 03:19 AM 0
Share

Well i thought that i could change it a bit to be relative to the player position/orientation, but i couldn't do anything working with it... :/

But for more explanation, here is what i mean: http://img541.imageshack.us/img541/3854/ka7j.png

So technically i thought i cold store the angles of the player body, then calculating the camera localeulerangles to be in -60 and +60, and then adding back the player angles, but sadly, it failed...

But forget about the planet thing, i wasn't clear about that but the player body stays perpendicular to the planet already.

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

Camera shows objects in wrong order 1 Answer

Let character walk on walls 1 Answer

Camera setup question 2 Answers

Face object towards a "direction" Crazy Taxi style 1 Answer

Adding a force(gravity) to a planet. 0 Answers


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