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 4t0m1c · May 13, 2015 at 09:49 PM · c#cameratouchcamera-movement

RTS touch camera movement

I've been trying to get my head around a solution for my camera movement. These are the parameters it needs to work by and some thoughts I had tried:

  • Camera movement / not world movement

  • Raycast to move camera accurately along terrain

  • Touch movement works by "grabbing" the terrain towards you or "pushing" it away

  • Screen space to world space will not work because of the angle of the camera

So my thoughts have lead me to devise a solution to grab the Vector3 point where the ray hits the ground under the finger on fingerDown then move the camera in an opposite direction to the real world distance between the origin and movingPoint. If that makes any sense.

I'm pretty confused about it so could someone either confirm or deny my idea and give a clear way forward =)

I'm using an asset called LeanTouch (brilliant piece of free scripts) for the touch inputs and this is as far as my brain wants to go: (lol)

 var finger = Lean.LeanTouch.Fingers [Lean.LeanTouch.Fingers.Count - 1];
 var ray = finger.GetRay ();
 int layerMask = (1 << 8);
 Physics.Raycast (ray, out hit, Mathf.Infinity, layerMask);
 InitialHit = hit.transform.position;

Btw i'm working in Unity and C#

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 4t0m1c · May 20, 2015 at 12:08 PM

Ok, I finally got it. A nice smooth accurate camera movement, based on the physical locations on the map from where my fingers touch the screen.

 using UnityEngine;

 public class SimpleCamMove : MonoBehaviour
 {
     private RaycastHit hit;
     private Vector3 InitialHit;
     private Vector3 CurrentHit;
     private Vector3 DirectionHit;
     private bool CamActive;

     protected virtual void OnEnable()
     {
         // Hook into the OnFingerDown event
         Lean.LeanTouch.OnFingerDown += OnFingerDown;
         
         // Hook into the OnFingerUp event
         Lean.LeanTouch.OnFingerUp += OnFingerUp;
     }

     protected virtual void OnDisable()
     {
         // Unhook the OnFingerDown event
         Lean.LeanTouch.OnFingerDown += OnFingerDown;
         
         // Unhook the OnFingerUp event
         Lean.LeanTouch.OnFingerUp += OnFingerUp;
     }

     public void OnFingerDown(Lean.LeanFinger finger)
     {
     
         var ray = finger.GetRay (); //fires ray with ScreenToWorld at finger pos using LeanTouch
         int layerMask = (1 << 8); //ground layer
         CamActive = true;
         if (Physics.Raycast (ray, out hit, 1500, layerMask)) {
             InitialHit = hit.point;
         }
     }
     
     protected virtual void LateUpdate()
     {
         if (CamActive == true) {
             var finger = Lean.LeanTouch.Fingers [Lean.LeanTouch.Fingers.Count - 1];
             var ray = finger.GetRay ();
             int layerMask = (1 << 8);
             if (Physics.Raycast (ray, out hit, 1500, layerMask)) {
                 CurrentHit = hit.point;
                 DirectionHit = (CurrentHit - InitialHit);
                 this.transform.Translate(new Vector3(-DirectionHit.x,0,-DirectionHit.z)); //camera stays at same height. Invert coords to move camera the correct direction
             }
         }
     }

     public void OnFingerUp(Lean.LeanFinger finger)
     {
         CamActive = false;
     }

 }
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

19 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

Related Questions

Camera Follow works partially, need help to finish the script 3 Answers

How do I program camera movement 2 Answers

My camera won't move properly. 1 Answer

What is the difference between gameobject movement speed by velocity and calculating the speed with deltatime and magnitude ? 1 Answer

moving Camera with touch constrained to up & down 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