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 venhip · Jan 23, 2014 at 10:59 PM · cameracamera.screentoworld

Terraria-esque 'scope' function? (2D)

Hey,

I've been developing this game of mine for a couple of months now and I've been managing surprisingly well, many thanks to Unity Answers, however, I've come across something I've been unable to solve. Much like the function of the sniper scope/rifle in Terraria I'd like to have the camera focus on a point about half-way between the cursor and the player. So far I've just managed to somehow make the player fly very fast in the Z axis.

For those who don't know what the scope is like:

When your cursor is on the player, the camera is centered, however, as you move your cursor outward the camera smoothly moves to roughly half way between the cursor and the player though the player is always in sight. Link because I doubt I've explained it well: Link!

Explanation end.

I would provide my code, however, I fear it is horrendously wrong, considering the player catapulting (I have no idea why it's happening.) It is probably best to start afresh with it, for me. Any insight on how to achieve such a thing would be excellent. Also, could it be in C# if possible. Though I'd be able to convert from JS, it'd be a little easier.

Thank you.

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
1
Best Answer

Answer by KellyThomas · Jan 23, 2014 at 11:46 PM

Well the focus point is just the mean of the player and mouse positions:

 public Transform player;
 public Transform camera;
 pubic float cameraLag = 0.5f;
 private float cameraZPosition;

 void Start() {
     cameraZPosition = camera.position.z;
 }
 
 void Update() {
     //find ideal point for cameraFocus 
     Vector3 mousePosition = camera.ScreenToWorldPoint(Input.mousePosition);
     Vector3 cameraFocus = (player.position + mousePosition) / 2;
     cameraFocus.z = cameraZPosition;
 
     //move camera closer to cameraFocus
     Vector3 offset = cameraFocus - camera.position;
     float ratioToMove = Mathf.Clamp(Time.deltaTime / cameraLag, 0.0f, 1.0f);
     camera.position += offset * ratioToMove;
 }
Comment
Add comment · Show 6 · 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 venhip · Jan 23, 2014 at 11:51 PM 0
Share

Ah, thanks. I attempted this before but I was obviously trying to over-complicate it. This is good thanks, however, the camera is moving in on the Z axis toward 0. Any idea why this is?

avatar image KellyThomas · Jan 24, 2014 at 12:04 AM 0
Share

Um... I 'm coding in browser not IDE and wont be home until this evening so there could be some kind of error i'm not spotting.

Try Debug.Log(cameraFocus) to see this that value is looking correct... that should help isolate any issues.

avatar image venhip · Jan 24, 2014 at 12:14 AM 0
Share

Thanks for the effort at least! The output of cameraFocus keeps changing. It starts at the original location then the cameraFocus.z tends to 0. I /nearly/ fixed it by adding to the z of cameraFocus, however, I end up with a jittering camera.

avatar image KellyThomas · Jan 24, 2014 at 12:18 AM 0
Share

Then we need to look into the value of mousePosition, if that is off then you will likely need to adjust the z component of the vector passed to `ScreenToWorldPoint()`.

avatar image venhip · Jan 24, 2014 at 01:08 AM 0
Share

Great, that has worked excellently! Though the focus is just on the player. The mouse doesnt seem to have factored into it. I tried Debug.Log-ing it, however, it returned values don't seem to correspond to the mouse's position. I can't figure out what it is supposed to be tracking. In the morning I'm going to try making a GameObject follow the mouse and use it's position ins$$anonymous$$d of the mouse. Thanks for all your help, and for putting me on track at least.

Show more comments

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

How to update ScreenToWorldPoint before Camera renders? 1 Answer

All Sprites Instantiate In top right of screen for some reason. 0 Answers

How to make camera position relative to a specific target. 1 Answer

Move character in direction its facing 1 Answer

Camera relative rigidbody third person player movement 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