Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Tylerdjo · Feb 03, 2018 at 07:41 PM · 3d5.3

Change target position of mouse orbit

I'm working with an edited version of the "mouse orbit improved" script (http://wiki.unity3d.com/index.php?title=MouseOrbitImproved). The target of the mouse orbit is set to a cube for testing purposes. Basically what I'm trying to do is give the player the ability to right click and change the position of the target (thus changing the pivot point of the orbit cam). I've managed to get the target to move around, but as far as I can tell it moves randomly relative to where I right click over my walking plane. However, if I print the hit data from the raycast I send out, it seems like numerically it IS placing the target transform at the correct position (i.e., if I get back a raycast of 1,2,0, the target cube moves to 1,20 - the problem is that this location is not where I have actually clicked on the screen). Something is clearly going on mathematically which I don't understand.

The relevant functions below are Update, SetTargetPosition, and Move.

 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
 public class MouseOrbitImproved : MonoBehaviour {
 
     public Transform target;
     Vector3 desiredPosition;
 
     public float distance = 15.0f;
     public float xSpeed = 120.0f;
     public float ySpeed = 120.0f;
 
     public float yMinLimit = 0f;
     public float yMaxLimit = 80f;
 
     public float distanceMin = 1f;
     public float distanceMax = 30f;
 
   
     private Rigidbody rigidbody;
 
     float x = 0.0f;
     float y = 0.0f;
 
     public Vector3 startingRot = new Vector3(45, 180, 0);
 
     // Use this for initialization
     void Start () 
     {
 
         distance = 15f; 
         xSpeed = 0f;
         ySpeed = 0f; 
      
 
         rigidbody = GetComponent<Rigidbody>();
 
         // Make the rigid body not change rotation
         if (rigidbody != null)
         {
             rigidbody.freezeRotation = true;
         }
 
         x = startingRot.y;
         y = startingRot.x; 
     }
 
     void Update () {
 
         if (Input.GetMouseButtonUp(1))
         {
             SetTargetPosition();
         }
     }
         void SetTargetPosition()
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit, 1000))
             {
                 desiredPosition = hit.point;
             Debug.Log(desiredPosition);
 
             }
             Move();
 
         }
 
         void Move()
         {
 
             target.position = desiredPosition; 
 
         }
 
 
 
    
    public void resetZoom ()
 
     {
         distance = 15f;
       
 
     }
 
     public void stopRotating () {
 
         xSpeed = 0f;
         ySpeed = 0f;
      
 
     }
 
     public void resumeRotating () {
         //being used?
         xSpeed = 40f;
         ySpeed = 80f;
       
 
     }
 
   
 
     void LateUpdate () 
     {
         if (target) 
         {
           
             Quaternion rotation = Quaternion.Euler(y, x, 0);
 
             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
             Vector3 position = rotation * negDistance + target.position;
 
            
             transform.rotation = rotation;
             transform.position = position;
         }
 
         if (target && Input.GetMouseButton(0)) 
         {
             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);
 
             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);
     }
 
     public void ChangeDistance(float dist) {
         dist = 1.0f-dist;
         float desiredDistance = (dist * (distanceMax - distanceMin)) + distanceMin;
         distance = desiredDistance;
     }
 
 }
 

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

0 Replies

· Add your reply
  • Sort: 

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

82 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 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 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 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 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

Sprites: Project Sprite to Mesh, allow for bending of sprites for 3d environments 0 Answers

Dice Value 1 Answer

Do I make an isometric game in 2D mode or 3D mode? 1 Answer

How do I make a 3D text a button 2 Answers

Why Do I Suddenly Have This Problem? 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