Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 buroks44 · Dec 13, 2021 at 09:13 PM · camera3dcamera-movementclick and drag

Click and Drag 3D Camera, like Anno

I'm trying to create a 3D camera that works just like in Anno and many other building/strategy games. I've been only learning Unity and C# for a couple of weeks so any extra help and clarifications would be very appreciated.

The basic logic for the camera: 1. Calculate mouse position on a ground layer with Ray. 2. On the right mouse click record the old mouse position. 3. While dragging, record the new mouse position by subtracting the old mouse position from the current mouse position. 4. Set GameObjects position (the parent of the Camera) by adding a new mouse position and multiplying it by Time.deltaTime.

The problem with my approach is that the position keeps adding up as long as the MRB is pressed down, even if the mouse isn't moving. Plus, the movement smoothing applies at the beginning and not when the mouse is released (ideally, no smoothing while dragging, and some subtle smoothing when released).

The only solution that I can imagine is seeing if the new mouse position actually updates or stays the same, but I have no idea how to do it as it is in the Update and keeps updating rapidly. So I imagine there must be a better way to do this.

Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraHandler : MonoBehaviour
 {
     // For MousePosition3D
     [SerializeField] private Camera mainCamera;
     [SerializeField] private LayerMask layerMask;
 
     // For Position
     private Vector3 oldMousePosition;
     private Vector3 newMousePosition;
    
     private void Start() {
         transform.position = transform.position;
         transform.position = Vector3.zero;
     }
 
 
     private void Update() {        
 
         // Position
         if (Input.GetMouseButtonDown(1)) {
             oldMousePosition = GetMouseWorldPosition();
         }
         if (Input.GetMouseButton(1)) {
             newMousePosition = GetMouseWorldPosition() - oldMousePosition;
             transform.position += newMousePosition * Time.deltaTime;
         }
     }
 
 
     private Vector3 GetMouseWorldPosition() {
         Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out RaycastHit raycastHit, float.MaxValue, layerMask)) {
             return raycastHit.point;
         } else {
             return Vector3.zero;
         }
     }    
 }
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

Answer by buroks44 · Dec 13, 2021 at 09:41 PM

Ok, so I added Vector3 initialTransform, set it to transform.position on GetMouseButtonDown and then calculated transform.position = initialTransform + newMousePosition * cameraMovementSpeed. Now the camera stops moving when I stop moving the mouse.

However, new issue is that the values on transform get way too accurate and starts causing errors. If I hold the RMB and don't move the house transform values keep changing with tiny changes and at some point the camera starts shaking. I think its accuracy is due to how the mouse position is calculated with Ray. How do I simplify that to a less accurate vector?

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

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

Jerky 3rd Person Camera Following Movement and Rotation 0 Answers

Moving between two cameras 1 Answer

Panning camera based on a fixed point. 0 Answers

How to make a 3D camera rotate relative to the world? 1 Answer

Screen is 2 cameras with each different properties. 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