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
1
Question by leonalchemist · Dec 19, 2011 at 11:38 PM · cameraclamprestrictions

Player in center of screen and camera restriction

k so 2 details, game is 2D bird's eye view shooter, and the code below is unfinished.

Basically wat i want to do is have my player always in the center of the screen unless i go near the edge of the screen(similar to as in Mario when u try to go to the left side of the screen the camera doesnt follow u anymore unitl u get back to the center of the screen)

for that, all i do is remove the camera object from the parent object into its own space showing in the code below. this code work but only when i got to the right side of the screen for some reason.(i know i havent coded for the y axes but if i do then it doesnt work at all)

any help?

also as u can see im using fixed number to say where the restrictions starts which wont work if u change resolution of the game so i need a fix for that too, i tried using screen.width and height but also didnt work.

 function LateUpdate () {
 
     transform.position = new Vector3(Mathf.Clamp(transform.position.x, -27, 27), Mathf.Clamp(transform.position.y, -36, 36), transform.position.z);
     
     if(player.transform.position.x <= Mathf.Clamp(transform.position.x, -27, 27))
     {
         transform.parent = player.transform;
     }
     else
     {
         transform.parent = null;
     }
 }
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 dannyskim · Dec 20, 2011 at 07:56 PM 0
Share

I would suggest not parenting the camera at all, just set the transform.position of the camera to follow the position of the character. Doing this has much more predictable results and isn't necessarily reasonably more expensive to do.

Also, take a look at the free Unity Lerpz platformer example, it has a pretty standard smooth follow camera script that you can create your basis off of.

http://unity3d.com/support/resources/tutorials/3d-platform-game.html

avatar image leonalchemist · Jan 28, 2012 at 10:04 PM 0
Share

ok late reply but i checked the Lerpz game and the code seems rather complicated + since it deals with changing camera restrictions and changing players it makes it even harder to understand. i tired fixing some things but im very much stuck right now cause there's more problems with the way im doing things as well. i dont usually ask for that much but i dont rly know where i should start, will try look more into that other game but dunno if im gonna gonna make sense of it much; but essentially the behavior of my camera is basically the same as the #D platform tutorial game

1 Reply

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

Answer by doomprodigy · Jan 29, 2012 at 01:49 AM

What I would do as dannyskim said is move the transform position of the camera to the character. So On a script that is attached to the player add this:

 void Start () {
     mainCamera = (GameObject) GameObject.FindWithTag ("MainCamera");
     
     }
 void Update () {
     mainCamera.transform.position = new Vector3(transform.position.x,10,transform.position.z);
     }

And then on a script attached to the main camera add:

 transform.position = new Vector3(Mathf.Clamp(transform.position.x, leftLimit, rightLimit), transform.position.y, transform.position.z);

In the Update somewhere, where left limit and right limit are public floats you have defined at the top of the script.

Note that this is done in C# but is very easily converted to Unity Script, if you look at the documentation.

Peace,

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 leonalchemist · Jan 29, 2012 at 12:27 PM 0
Share

thx, after a couple tweaks i kinda made it work.. well it works but it messes up 2 other things. um wat i changed in the update was

mainCamera.transform.position = new Vector3(transform.position.x,transform.position.y,-14);

because im using the x and y to move up and down, just makes more sense to me and on my camera script

transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, -27.2, 27.2), $$anonymous$$athf.Clamp(transform.position.y, -35.7, 35.7), transform.position.z);

because the camera restriction has to work with the y axes as well.

EFIT: removed previous problem cause it was an easy fix

and i have another script that shows the damage on enemies head like an rpg but when i reach the side of the screen, the number kinda go the opposite direction of the player

its either gotta do with those 2 lines i think...

function Start () { GUIDamagePosition = Camera.main.ViewportToWorldPoint(gameObject.transform.position); }

function Update() { //Convert WorldToViewportPoint to a vecotr3 so the GUIText position remains on the enemy transform.position = Camera.main.WorldToViewportPoint(GUIDamagePosition); }

i'll obviously look into it myself but i wouldnt be against some help cause this seems a bit confusing

lol gigantic cooment :/

avatar image doomprodigy · Jan 29, 2012 at 11:12 PM 0
Share

No problem. I suggest taking the answer as the correct one to declutter the unanswered and if you have any other problems regarding a different part of coding to make a new question so that the rest of the UA community can help answer it for you.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with third-person camera stuttering involving raycasts and clamp 0 Answers

Problems with 2D Camera Shaking 3 Answers

Rotating and orbiting an game object with a stop angle 0 Answers

How can I clamp a GameObject to screen borders? 2 Answers

Camera Reset! please help me to solve this. 2 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