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 Ivakha · Apr 21, 2016 at 05:57 PM · 2d-platformercamera follow

[2D platformer] Placing camera between player and mouse

Hello! In my 2D game camera follows the cursor, and player stays visible. Here is my script:

 Vector3 position = (target.position + camera.ScreenToWorldPoint(Input.mousePosition)) / 2f;
 Vector3 destination = new Vector3(position.x, position.y, -10);
 transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);

When i get my cursor on the edge of the screen (for example (1,1) in ViewPortPoint coordinates), the player is barely visible. I want to set maximum value for cursor's position. For example (1,1) will be the same as (0.8, 0.8) so the whole player is always visible.

Thank you for your attanetion!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Jessespike · Apr 21, 2016 at 09:55 PM

Replace Input.mousePosition with a new Vector3 that consists of Input.mousePosition with screen size calculations.

 float maxScreenPoint = 0.8f;
 Vector3 mousePos = Input.mousePosition * maxScreenPoint    + new Vector3(Screen.width, Screen.height, 0f) * ((1f - maxScreenPoint) * 0.5f);
 //Vector3 position = (target.position + GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition)) / 2f;
 Vector3 position = (target.position + GetComponent<Camera>().ScreenToWorldPoint(mousePos)) / 2f;
 Vector3 destination = new Vector3(position.x, position.y, -10);
 transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);

Comment
Add comment · Show 1 · 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 Ivakha · Apr 22, 2016 at 06:40 AM 0
Share

Thank you so much for the extended help! This is exactly what i needed

avatar image
-1

Answer by DageLV · Aug 01, 2018 at 07:38 AM

Soo... How do i make this script work? I copy/paste the code in unity and i just get compiler errors. Anyone knows how to make the script actually work?

Comment
Add comment · Show 1 · 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 PaulKhill · Sep 04, 2020 at 02:29 PM 0
Share

Quite easy actually. Read the script, make sure you undersand it. Add the couple variables that are missing. Read the errors you get and fix them. $$anonymous$$ake sure that it is called upon the correct object (players & cameras) and voilà, working like a charm, and smoothly as it is !

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

59 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

Related Questions

Camera not following 2D character when the character reaches the ground. 0 Answers

How Do I Lock The Y Axis In A Camera Follow Script? 1 Answer

I'm making a 2d platformer. I got the camera to follow me, but when I jump up the whole camera goes up. How do I make the camera stay focused in on one view instead of going up and down? 0 Answers

When player move the camera flickers and it looks janky 0 Answers

Unity2D Pixel Perfect Camera: Buggy Camera 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