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 W1k3 · Nov 26, 2013 at 05:38 AM · camerarotationpositionquaternionlogic

Help creating restraints for an objects movement according to it's parent

I couldn't come up with a great title, but my situation is messing with my head. My game is a space game where you control a plane in third person. The camera is using this orbit script:

[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")] public class mouseorbit : MonoBehaviour {

 public Transform target;
 public float distance = 5.0f;
 public float xSpeed = 120.0f;
 public float ySpeed = 120.0f;
 
 public float yMinLimit = -20f;
 public float yMaxLimit = 80f;
 
 public float distanceMin = .5f;
 public float distanceMax = 15f;
 
 float x = 0.0f;
 float y = 0.0f;
 
 // Use this for initialization
 void Start () {
     Vector3 angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
     
     // Make the rigid body not change rotation
     if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 void LateUpdate () {
     if (target) {
         x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
         
         y = ClampAngle(y, yMinLimit, yMaxLimit);
         
         Quaternion rotation = Quaternion.Euler(y, x, 0);
         
         distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
         
         RaycastHit hit;
         if (Physics.Linecast (target.position, transform.position, out hit)) {
             distance -=  hit.distance;
         }
         Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
         Vector3 position = rotation * negDistance + target.position;
         
         transform.rotation = rotation;
         transform.position = position;
         
     }
     
 }
 
 public static float ClampAngle(float angle, float min, float max)
 {
     if (angle < -360F)
         angle += 360F;
     if (angle > 360F)
         angle -= 360F;
     return Mathf.Clamp(angle, min, max);
 }

}

I have a unique aiming mechanic. There is a cross hair across from the camera on the other side of the ship as a parent of the camera. The gun points use look at to aim at the crosshair

alt text

This way, Where ever I orbit the camera, the cross hair is in the middle farther away.

alt text

The issue with this is that guns can't shoot backwards. I need to constrain how far the cross hair can point away from straight forward. I want the camera to still be able to look backwards, while the cross hair gets as close as it can to being in your view; in turn, preventing the player from shooting at too much of an angle.

http://i.imgur.com/S5GopU9.jpg (ran out of attachments)

Unfortunately, I can't even begin to think of how this could be accomplished in unity. So any Advice, ideas, alternatives or solutions would superb.

Thanks.

ex1.jpg (58.1 kB)
ex2.jpg (59.4 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

1 Reply

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

Answer by Tomer-Barkan · Nov 26, 2013 at 06:28 AM

You could keep two separate angles, one for the camera, and one for the crosshairs. So the camera can move freely 360, but the crosshairs will be calculated according to the camera.

If the camera is between -30 and 30 (say 30 is the maximum angle of the guns) from the ship's forward vector, then the crosshairs angle remains exactly like the camera angle. If the camera is between 30 and 180 degrees from the ship's forward, then the crosshairs remains 30. If on the other hand it is between -30 and -180 (or 180 and 290, which is the same) then your crosshairs remain -30.

You can calculate the crosshairs angle every frame based on the camera angle.

Some sample script:

 public Vector3 GetCrosshairsPosition(Transform ship, Transform camera, float maxAngle, float distanceFromShip) {
     Vector3 directionFromShip = Vector3.RotateTowards(ship.forward, camera.forward, Mathf.Deg2Rad * maxAngle, 0);
     return ship.position + directionFromShip * distanceFromShip;
 }
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 W1k3 · Nov 27, 2013 at 03:19 AM 0
Share

I'm not smart enough to understand this. Once I'm better at program$$anonymous$$g and trigonometry I'll come back to this. I'll mark you as the rite answer :)

avatar image Tomer-Barkan · Nov 27, 2013 at 06:14 AM 0
Share

lol sorry about that. You definitely need a basic understand of 3d geometry to use unity well.

I'll try to explain a bit more:

The ship is pointing forward. The camera is rotated so that it is looking from right to left. So the ship's forward vector is pointing straight forward, and the camera's forward vector is pointing straight left. Hence the angle between them is 90 degrees.

Now if the guns can only fire 30 degrees, the vector pointing from the ship to the crosshairs cannot be the same as the one of the camera, because that would be 90 degrees. So ins$$anonymous$$d, you take the ship's forward vector (the one pointing straight ahead), and you rotate it towards the camera's vector (the one pointing left), but you limit the rotation to a maximum of 30 degrees. (thats the 3rd parameter of Vector3.RotateTowards).

What you get is a vector that is rotated towards the direction the camera is looking, but no more than 30 degrees, which means it is within the gun's range.

Then it is a simple matter of starting from the ship's position, then moving X units towards the calculated vector, and you get the location of the crosshairs. (X being distanceFromShip, the third parameter to the function)

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

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

Related Questions

How to make a camera "point" at an object. 1 Answer

How to force camera rotation? 1 Answer

How to make Camera position Independent of its Rotation? 1 Answer

Vector3.Lerp moves other characters over network to (0, 0, 0) 1 Answer

How to apply a force forward depending of the camera 2 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