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 LiamSorta · Sep 07, 2015 at 03:49 PM · cameracamera movementclamptrackingdamping

[2D - Top Down] Camera track / Border control

Hey, I'm having a bit of an issue with my camera script. The aim is so that it will gently track the player up until it hits the background boundary. I can get it to track the player statically and stop at the walls, I can slowly track the player, but I can't do both.

Here's what I'm using now:

 using UnityEngine;
 using System.Collections;
 
 public class Border : MonoBehaviour
 {
     private float rightBound;
     private float leftBound;
     private float topBound;
     private float bottomBound;
     private Vector3 pos;
     private Transform target;
     private SpriteRenderer spriteBounds;
     public float dampTime = 0.15f;
     private Vector3 velocity = Vector3.zero;
 
     // Use this for initialization
     void Start()
     {
         float vertExtent = Camera.main.orthographicSize;
         float horzExtent = vertExtent * Screen.width / Screen.height;
         spriteBounds = GameObject.Find("BG").GetComponentInChildren<SpriteRenderer>();
         target = GameObject.FindWithTag("Player").transform;
         leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
         rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
         bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
         topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
     }
 
     void FixedUpdate()
     {
         Vector3 point = Camera.main.WorldToViewportPoint(target.position);
         Vector3 delta = target.position - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z)); 
         Vector3 destination = transform.position + delta;
 
         var pos = new Vector3(target.position.x, target.position.y, transform.position.z);
         pos.x = Mathf.Clamp(pos.x, leftBound, rightBound);
         pos.y = Mathf.Clamp(pos.y, bottomBound, topBound);
         //transform.position = pos;
         Debug.Log(pos);
         transform.position = Vector3.SmoothDamp(pos, destination, ref velocity, dampTime);
         
     }
 
 
 }

 

If anyone could shed some light as to why this is happening I'd be very grateful. Thanks!

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

Answer by cjdev · Sep 07, 2015 at 09:49 PM

You're Clamping the position before the SmoothDamp. Try doing it to the transform's position after the SmoothDamp.

Comment
Add comment · Show 3 · 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 LiamSorta · Sep 08, 2015 at 02:11 AM 0
Share

Just gave it a try and I'm getting the same issue, I'm clamping the target positions so it's ignoring the current transform. I think my general theory on how this was supposed to work is wrong, I'm certain there's a better way of doing it, I can't seem to find one though.

avatar image cjdev · Sep 08, 2015 at 06:35 AM 0
Share

You could try clamping the destination vector ins$$anonymous$$d of the current position.

avatar image LiamSorta cjdev · Sep 16, 2015 at 11:45 AM 0
Share

Sorry just came back home! Tried clamping the destination and works perfectly, albeit with a bit of jitteriness but I can work on that. Thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

First person camera snaps when switching cameras 0 Answers

How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers

Clamp camera within bounds of a Polygon Collider? 1 Answer

Touch camera movement isn't smooth on Android 0 Answers

Rotate object from headmovement using webvr?,Rotate object from head tracking movement? usingWEBVR asset store? 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