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 LWKatze · Mar 23, 2016 at 12:12 PM · camera movementtrigonometryinput.mousepositionmouse dragcamera.screentoworld

Scale Z Axis Movement According to Camera Tilt (Drag Control Camera)

Hello, I've been looking online for a solution for this problem but haven't been able to find one. I'm attempting to create a script to control camera movement on a mobile device, though right now I'm using the mouse as a substitution for touch in the editor. The camera is looking down on a quad at an angle, similar to the way the camera functioned in the game Age of Empires II. The drag works as expected when positioned at a 90˚ tilt in the x axis and looking straight down at the quad––a movement of one world space unit in either direction of the mouse corresponds to one world space unit of movement in the camera. When the camera is tilted, say, at 45˚I expect to scale the amount of movement of the camera in the z axis by a certain amount, at 45˚ I would expect the scale value to be the sqrt(2). However, after testing out various values through trial and error, I need to scale it by 2. At 30˚ it's 4, and at 15˚ tilt I scale the z axis movement by 15˚ relative to the z value from Camera.main.ScreenToWorldPoint(Input.mousePosition). I would like to have the ability to tilt the camera at any angle, but I have no idea how to calculate the scalar value. Is there a way to calculate it, or is there a better way to go about doing this? Thank you so much in advance for any answers, and sorry for the long-winded explanation. Oh, and the camera is in orthographic mode.

Tldr; I want to "drag" a camera around the screen looking down at a quad at an angle, but can't find the proper way to scale the mouse's z position to the camera's z position.

Explanatory Youtube Video

Code:

//this is attached to the main camera. These functions are called from a separate script that handles input.

using UnityEngine; using System.Collections;

public class CameraCtrl : MonoBehaviour {

 //Methods for editor testing
 public Vector3 mouseOrigin;    //this serves as the initial position of the mouse to calculate the movement over one frame

 //method is called when a click begins before DragEditor (Touch[] touch).  The Touch[] argument goes unused in both functions.
 void SingleTouchDownEditor(Touch[] touch)
 {
     mouseOrigin = Input.mousePosition;    //set the mouse origin
 }

 public Vector3 dragThreshold = new Vector3(0f, 0f, 0f);
 public Transform cube;
 public float zValueScaler;
 void DragEditor(Touch[] touch)
 {
     Vector3 pos;
     Vector3 move;
     if((Mathf.Abs(Input.mousePosition.x - mouseOrigin.x) > dragThreshold.x) || (Mathf.Abs(Input.mousePosition.y - mouseOrigin.y) > dragThreshold.y))
     {
         pos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.ScreenToWorldPoint(mouseOrigin);
         move = new Vector3(pos.x, 0, pos.z * zValueScaler);
         Debug.Log("Current MousePos: " + Camera.main.ScreenToWorldPoint(Input.mousePosition));
         Debug.Log("Camera Pos: " + Camera.main.transform.position);
         Camera.main.transform.Translate(-1 * move, Space.World);
     }
     float speed = 0.5f;
     mouseOrigin = Input.mousePosition;    //set mouseOrigin before the next frame
 }

}

alt text alt text

imageone.png (171.2 kB)
imagetwo.png (223.5 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

ScreenToWorldPoint returning weird value. 2 Answers

(solved) 2d sprite rotation towards mouse 1 Answer

World point for MousePosition 1 Answer

Cant Rotate in Unity Survival Shooting Game tutorial 0 Answers

Math question, angles and magnitude stuff 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