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 /
avatar image
0
Question by lonelycamper · Jun 18, 2019 at 08:02 PM · cameratopdown

TopDown shooter camera

Need a Camera to look at the player and move the camera to the mouse cursor while keeping the player in view I don't really understand how I would do this 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 metalted · Jun 18, 2019 at 08:40 PM 0
Share

Could you clarify your idea a bit more ? I'm not really getting a clear picture around this idea. If you give some more information in your question I'm sure we can find something that suits your needs.

avatar image lonelycamper metalted · Jun 18, 2019 at 08:58 PM 0
Share

Like the camera moves to the cursor but keeps the character still in view of the camera for example, if I move the cursor in front of the camera it moves slightly forwards

Shows in games like Hotline $$anonymous$$iami or enter the gungeon

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zarenityx · Jun 18, 2019 at 09:36 PM

There's a lot of ways one could do this, but here's one:

  • Find the XZ world position of the cursor. You could use Camera.ScreenToWorldPoint for this, or, if you're already doing a raycast for selections or the like, you could use that. You just need the XZ position, since it's a top-down 2D game.

  • Get the difference between this position and the player's position. Along the lines of diff = cursorPos - player.transform.position; where diff and cursorPos are of type Vector3, and player is your player reference (GameObject of Script).

  • Declare a public float maxDist; that will represent how far away the camera is allowed to drift from the player. This value will depend on the size of your camera, environment, and how much of an effect you want the mouse to have overall. Optionally declaring a 'public float offsetAmt;' can let you minimize or exaggerate the offset effect as desired.

  • Apply clamping to the magnitude, and optionally multiply by the offsetAmt if you're using it. Like so: diff = Mathf.Clamp(diff.magnitude*offsetAmt,0,maxDist)*diff.normalized;

  • (Optional) If you want it to be a smoother transition, you might want to declare a public float lookSmoothSpeed; and a Vector3 oldDiff; somewhere and include the line diff = Vector3.Lerp(oldDiff,diff,Time.deltaTime*lookSmoothSpeed); oldDiff = diff;' This will make the offset of the camera smoothly approach the correct value, slowing down as it gets closer, rather than just snapping to it, and can feel better. Experiment with values for lookSmoothSpeed`, although I recommend starting off around 10. Note that when you start the script, it's probably a good idea to set oldDiff = player.transform.position;.

  • Apply the offset to the camera. Along the lines of Vector3 campos = player.transform.position+diff; campos.y = camera.transform.position.y; camera.transform.position = campos;



The result should be a camera that moves a bit like how you want, although I should state as a disclaimer that nothing I just wrote is tested, and I wrote this all off the top of my head. In any case, this should get you headed in the right direction.

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 lonelycamper · Jun 18, 2019 at 10:51 PM 0
Share

This is a 3d game, not a 2d game does the same concept apply?

avatar image Zarenityx lonelycamper · Jun 20, 2019 at 12:16 AM 0
Share

Ah. Sorry you mentioned Hotline $$anonymous$$iami and Gungeon and I assumed 2D. As long as camera movement is all top-down, i.e. constrained to the XZ plane, then yes this definitely works. If you're doing a perspective camera and moving it up and down to look closer or farther then you'd need to scale the maxDist and offsetAmt accordingly. For cameras that are 'mostly' top-down, but rotate a bit, this technique may or may not be valid, depending on the desired game feel and the amount of rotation.

If the camera movement can be considered 2D for all intents and purposes, even if rendering is 3D, then yes, there is value in this technique.

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

170 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 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 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

Proper/Standard way to create a top down / isometric sprite sorting solution 1 Answer

I am writing a camera script, but it seems not to be working. Any suggestions? 1 Answer

2D Top Down Camera Smoothing 1 Answer

How to Move Top Down Camera Towards Game Object on Touch? 1 Answer

Deadfrontier type crosshair 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