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 peter_kes · Oct 04, 2014 at 02:10 PM · cameradrag

Drag an object which camera follows

When you drag an object(http://answers.unity3d.com/questions/566327/drag-object-relative-to-camera.html) , while the program lets camera follow the object (by making the camera child object of the object or a script that follows the transform of the object), the dragged object moves in abruptly quickly. Is there any methods which allow the dragged objects is followed by the camera in normal way? I tried to limit maximum movement of the camera by using various ways such as Mathf.MoveTowards etc.. but no success. Thanks

Comment
Add comment · Show 2
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 peter_kes · Oct 04, 2014 at 11:13 AM 0
Share

I basically want to make a Side Scrolling games like $$anonymous$$ario but left and right movement by dragging by finger/mouse

avatar image KayelGee · Oct 06, 2014 at 12:32 PM 0
Share

$$anonymous$$oveTowards should do what you want. Look here. I would like to see what your attempt with $$anonymous$$oveTowards was so I can tell you what you did wrong.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by peter_kes · Oct 07, 2014 at 11:04 AM

For the dragged Object (Could be more sofisticated though):

Vector3 initialPos; Vector3 curPos; float speed = 3f;

 void Update () {
     if(Input.GetMouseButton(0)){
         curPos = Input.mousePosition;
     }
 }

 void OnMouseDown() {
     initialPos = Input.mousePosition;
 }
 
 void OnMouseDrag() {
     if(curPos.x != initialPos.x ) 
     {
         transform.position = new Vector3(transform.position.x +(curPos.x - initialPos.x) * Time.deltaTime * speed,  transform.position.y, transform.position.z);
     }
 }

For the camera: public Transform target; public float smoothTime = 0.3F; private Vector3 velocity = Vector3.zero;

 void Start () {
     transform.position = new Vector3 (target.transform.position.x,
                                       target.transform.position.y, 
                                       transform.position.z);
 }
 
 void Update () {
     transform.position = Vector3.SmoothDamp(transform.position, target.position, ref velocity, smoothTime);
     transform.position = new Vector3(transform.position.x, the y position that it should stay as this is horizontal scrolling, -10f);
 }
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
avatar image
0

Answer by Andres-Fernandez · Oct 06, 2014 at 01:28 PM

Instead of moving the object directly with the finger drag, move an invisible mock object instead and then make the object follow that mock object's position using SmoothDamp, or lerp, or anything similar. When adding smoothness to movement (either to a camera or any other object that doesn't need to be time controlled) I prefer using the SmoothDamp option.

[Edit] Unless the object has a speed parameter (i.e. time controlled). Then use lerp instead, it's easier to tweak.

Comment
Add comment · Show 2 · 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
avatar image peter_kes · Oct 07, 2014 at 07:13 AM 0
Share

thank you for the answer. The camera movement using SmothDamp is very smooth to follow the object. The actual issue was not that though, but I found out that because $$anonymous$$ouseInput/Touch point is first calculated into unity world position using Camera.main.ScreenPointToRay. Camera position also moves along with the dragged object in my program, and the problem is that every dragging position is calculated in according to the moving Camera position due to the Camera.main... the dragged object moved too much. so I made it so that drag is done without using Camera.main.ScreenPo... .

avatar image Andres-Fernandez · Oct 07, 2014 at 07:20 AM 0
Share

Did you solve it? Can you post how you did it? It may help others with the same problem (and I'm curious about it).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The solution with camera rotation which is used in most strategic games 1 Answer

Rotating camera with RMB drag 1 Answer

How to have Camera following a Target, but not directly centered? 3 Answers

Mouse/Wacom stylus drag not working. 1 Answer

Dragging camera around object using mouse issue 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