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
3
Question by miklossmusch · Dec 08, 2014 at 10:03 AM · camera-lookdraggingmouse-dragpath-finding

Swipe/Drag to move Camera along Path/Track

Hello all,

I'm having trouble finding some specific information in regards to this question, and I would greatly appreciate it if anyone could spare to time to explain. Thanks in advance!

I'm looking to move the Camera along a path when touch/drag input has been received. I understand you can move the camera along a certain axis, but what about a predefined track so that the camera can't be dragged off into oblivion?

My scene is like this: There is a wall which meanders around a house with various points of interest along the wall. Here is a sketch to explain a little further: alt text

Sometimes the roof is a point of interest, so the camera needs to be able to look up but sometimes the roof isn't present so it needs to in some places be locked to only view the walls.

Does anyone know how to give the camera a certain 'allowed' track that it can stick to? Can this be

Here is an example of the swipe feature i'm looking for alt text

If anyone has any idea how to do this and can explain your code as you go that would be incredible! Thank you all!

swipeexample.jpg (84.4 kB)
swipeexample2.jpg (115.9 kB)
Comment
Add comment · Show 1
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 CHPedersen · Dec 08, 2014 at 10:04 AM 0
Share

GREAT question, with EXCELLENT explanatory image material. All immediately understandable; a lot of askers on this site could learn from you. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CHPedersen · Dec 08, 2014 at 10:33 AM

Here's an idea that might help you:

To solve this one, you could define a number of control points along the floor, following the edge of the wall. These would define a curve that determines the cameras's sandbox: the area within which it is allowed to navigate. Each control should consist of a position (Vector3) and a rotation (Quaternion). You could define a struct or a class called "CameraNavigationControlPoint" or "CamNavControlPoint" or whatever you wish, to combine these two pieces of data.

The total navigable area would thus comprise a list of these. The idea is to maintain an index into this list that determines the two control points in the curve, between which the camera is currently located. A linear interpolation between the position and rotation of these two control points will then tell you what the camera's current position and rotation should be.

I cooked up a script briefly which, while it certainly compiles, is untested and serves only to demonstrate my idea in code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public struct CamNavControlPoint
 {
     public Vector3 Position;
     public Quaternion Rotation;
 }
 
 public class CameraNavigation : MonoBehaviour {
 
     // A floating point 'index' into the list of ControlPoints. Use this to lerp your camera's position and rotation out of the list!
     private float curCamIdx = 0;
 
     private List<CamNavControlPoint> ControlPoints = new List<CamNavControlPoint>();
 
     // Use this for initialization
     void Start () 
     {
         // Add control points that define the curve in front of the wall, as well as a series of rotations that represent pointing the forward vector at the wall
         // This is all example data, your positions and rotations are for you to figure out
         ControlPoints.Add(new CamNavControlPoint() { Position = new Vector3(0, 0, 0), Rotation = Quaternion.Euler(Vector3.zero) });
         ControlPoints.Add(new CamNavControlPoint() { Position = new Vector3(1, 0, 0), Rotation = Quaternion.Euler(Vector3.zero) });
         ControlPoints.Add(new CamNavControlPoint() { Position = new Vector3(2, 0, 0), Rotation = Quaternion.Euler(Vector3.zero) });
         // etc...
     }
 
     Vector2 swipeThreshold = new Vector2(0, 0); // Whatever
 
     // Update is called once per frame
     void Update () 
     {
         Touch t = Input.GetTouch(0); // Only one touch?
         if (t.deltaPosition.magnitude > swipeThreshold.magnitude)
             Swipe(t.deltaPosition.x); // I'm guessing this is negative if left, positive if right
     }
 
     private void Swipe(float swipeVal)
     {
         curCamIdx += swipeVal;
 
         int floored = Mathf.FloorToInt(curCamIdx);
         int ceilinged = Mathf.CeilToInt(curCamIdx);
 
         Camera.main.transform.position = Vector3.Lerp(ControlPoints[floored].Position, ControlPoints[ceilinged].Position, curCamIdx - floored);
         Camera.main.transform.rotation = Quaternion.Lerp(ControlPoints[floored].Rotation, ControlPoints[ceilinged].Rotation, curCamIdx - floored);
     }
 }

Take a look at that and try and understand what that code does. It's pretty simple. :) Hopefully it leads you on the right track, at least. Bear in mind, like I said, that this script demonstrates the idea only. I did not, for example, set the swipeThreshold, which is the minimum distance we want the user's finger to have travelled before triggering the swipe. In addition, I took the liberty to assume that the swipe direction will have negative x to the left and positive to the right. Maybe that isn't the case. Similarly, the x distance swiped probably doesn't translate directly to how far the controls should interpolated between each consecutive points in the sequence. You'll have to play with those values yourself.

The neat thing about this idea is that it will automatically interpolate a smooth transition between two right-angled rotations when two walls meet at a corner. You can play with putting points closer together there than elsewhere, to precisely define the bend, for example.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Script for camera movement using Arcball,Scripting Camera Movements with Arcball 0 Answers

How to drag object in direction of camera? 1 Answer

Camera move on mouse click 2 Answers

Right click drag and drop script 1 Answer

Tracking slow mouse movements 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