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 /
  • Help Room /
avatar image
0
Question by WhyldH · Jun 05, 2016 at 12:07 AM · camera-movementanchortroubleshooting

[2D]Hybrid cursor and mouse following camera trouble [Resolved]

Here's the problemator :

 using UnityEngine;
 using System.Collections;
 
 public class HybridFollowCursor2DScript : MonoBehaviour
 {
     public Transform target; // Target on wich the camera will be anchored.
     public float maxDistance; // Longest distance the camera can get from the target.
     public float minHeight; // Lowest vertical level the camera can get.
     
 
     void Start ()
     {
         if (target == null)
             return; 
 
         Vector2 iniCamPos = new Vector2 (target.position.x, minHeight); // Creat the "Vector2" Type needed for ".MovePositon(Vector2)" function.
         rigidbody2D.MovePosition(iniCamPos); 
     }
 
 
     void FixedUpdate () 
     {
         Vector2 cursorPos = Input.mousePosition; // cursor position
         Vector2 trgPos = target.position; // target position
 
         // Distances between the cursor and the target.
         float dx = cursorPos.x - trgPos.x; // vertical distance
         float dy = cursorPos.y - trgPos.y; // horizontal distance
 
         // Use of another variable because we'll use logarithm right after that, and ".log" function can't handle negative numbers.
         float adx; // the horizontal distance we'll had between the camera and the target
         float ady; // the vertical distance we'll had between the camera and the target
 
         if (dx < 0)
             adx = - Mathf.Log (Mathf.Abs(dx)); // We're negating the value when the cursor is left-side of the target...
         else
             adx = Mathf.Log (dx); 
 
         if (dy < 0)
             ady = - Mathf.Log (Mathf.Abs(dy)); 
         else
             ady = Mathf.Log (dy); 
 
         float trueAdy = trgPos.y + ady; // Here we need to combine the calcul for the vertical height within one variable because the limitation for the lowest point doesn'twork the same as the other limitations.
         float trueAdx = trgPos.x + adx; // Not necessary at all, but maybe it'll do something positive ???
 
         if (adx > maxDistance)
             adx = maxDistance;
         else if (adx < - maxDistance)
             adx = - maxDistance;
         if (ady > maxDistance)
             ady = maxDistance;
         if(trueAdy < minHeight) 
             trueAdy = minHeight; // "minHeight" needs to block the camera on a certain camera height (void is only good for coders).
 
         // The True Line of Destiny ! ...
         rigidbody2D.position = new Vector2 (trueAdx, trueAdy); //... except it doesn't work... and I don't know why. T_T
     }
 }

The problem is that the camera's anchor isn't centered on the target but way on the right.... I've searched for more than 2 or 3 hours now and still don't know what's going on.

So please if someone have the answer, let me know (even more if you share tips to optimise this monstruosity).

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 WhyldH · Jun 05, 2016 at 04:13 PM

Here is the corrected camera, using lerp function for smoothing, instead of the crapy log system. ^^"

 using UnityEngine;
 using System.Collections;
 
 public class HybridFollowCursor2DScript : MonoBehaviour
 {
     public Transform target; 
     public float maxDistance; 
     public float minHeight; 
     public float Damping;
 
     void Start ()
     {
         if (target == null)
             return; .
 
         Vector2 iniCamPos = new Vector2 (target.position.x, minHeight);
         rigidbody2D.MovePosition(iniCamPos); 
     }
 
 
     void LateUpdate () 
     {
         Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); 
         Vector2 trgPos = target.position; 
         float currentx = transform.position.x;
         float currenty = transform.position.y;
 
         float dx = cursorPos.x - trgPos.x; 
         float dy = cursorPos.y - trgPos.y; 
         float camdx = currentx - trgPos.x;
         float camdy = currenty - trgPos.y;
         float wantedx = trgPos.x + dx;
         float wantedy = trgPos.y + dy;
 
         float trux = Mathf.Lerp (currentx, wantedx, Damping * Time.deltaTime);
         float truy = Mathf.Lerp (currenty, wantedy, Damping * Time.deltaTime);
         float basicx = Mathf.Lerp (currentx, trgPos.x, (Damping*2) * Time.deltaTime);
         float basicy = Mathf.Lerp (currenty, minHeight, (Damping*2) * Time.deltaTime);
 
         float effCamdx = Mathf.Log (Mathf.Abs (camdy));
         float effCamdy = Mathf.Log (Mathf.Abs (camdy));
         float truMxDistX = trgPos.x + maxDistance - effCamdy;
         float truMnDistX = trgPos.x - maxDistance + effCamdy;
         float truMxDistY = trgPos.y + maxDistance - effCamdy;
         float truMnDistY = trgPos.y - maxDistance + effCamdy;
 
         if (trux >= truMxDistX)
             trux = truMxDistX;
         else if (trux < truMnDistX)
             trux = truMnDistX;
         if (truy >= truMxDistY)
             truy = truMxDistY;
         else if (truy < truMnDistY)
             truy = truMnDistY;
 
         if (truy < minHeight)
             truy = minHeight;
 
 
         if (dx > maxDistance / 1.2 || dy > maxDistance / 1.2 || dx < -(maxDistance / 1.2) || dy < - (maxDistance / 1.2))
             rigidbody2D.position = new Vector2 (trux, truy);
         else
             rigidbody2D.position = new Vector2 (basicx, basicy);
     }
 }

I've also added some little tweaks, to improve gameplay comfort. Maybe it'll inspire someone. ^^

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

60 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

Related Questions

Meaning of Dotted Rectangle Widget in GUI System 0 Answers

Error in camera script 1 Answer

how to jump my character to fixed distance 0 Answers

How to rotate a camera around a spaceship freely 0 Answers

Camera not following 2D character when the character reaches the ground. 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