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 Abdola · Jan 22, 2015 at 10:27 PM · camera2dcamera follow

Camera follow not working (deadzone)!

Hi everyone. I'm having some trouble with my camera movement and i'd appreciate some help. The idea is the camera moves towards the player after they leave a deadzone in the middle of the area, but lets them move a bit inside the deadzone. Like Super Mario World. This is my code as is, but the deadzone doesn't seem to work. The camera doesn't move until the player reaches the middle of the screen where the deadzone is.

 var Player: Transform;
 var xDifference: float;
 var yDifference: float;
 var moveThreshold: float = 3;
 var cameraSpeed: float = 3;
 var abovePlayer: float;
 
     function awake() {
     //references the player (player tagged object)
         Player = GameObject.FindWithTag("Player").transform;
     
     };
 
 
     function FixedUpdate() {
     //keeps z co-ordinate the same
     transform.position.z = -5;
     //keeps camera above the player slightly
     transform.position.y = Player.position.y + abovePlayer;
 
 
 
     //calculates the difference between the player's x co-ordinates and the camera's
     if(Player.transform.position.x > transform.position.x) {
     xDifference = Player.transform.position.x - transform.position.x;
 } else {
     xDifference = transform.position.x - Player.transform.position.x;
         
 };
 
 //calculates the difference between the player's y co-ordinates and the camera's
     if(Player.transform.position.y > transform.position.y) {
     yDifference = Player.transform.position.y - transform.position.y;
 } else {
     yDifference = transform.position.x - Player.transform.position.y;
         
 };
 
 
 //checks whether the player is beyond the deadzone threshold and moves the camera acordingly
     if (xDifference >= moveThreshold || yDifference >= moveThreshold) {    
         transform.position = Vector2.MoveTowards(transform.position, Player.transform.position,cameraSpeed*Time.deltaTime);
         transform.position.z = -5;
         transform.position.y = Player.position.y + abovePlayer;
         
 //prevents the camera from going beyond the start of the level
         if (transform.position.x < -2) {
             transform.position.x = -2;
         }
 
     };
     
     
 
 
 };
     

Help would be much appreciated!

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 khos85 · Jan 22, 2015 at 10:59 PM 0
Share

how have you setup the deadzone, e.g. an object with collider? or could you do it this way ins$$anonymous$$d of via code?

avatar image Abdola · Jan 23, 2015 at 09:01 AM 0
Share

The idea is the deadZone is calculated by the difference between the camera's coordinates and the player's

0 Replies

· Add your reply
  • Sort: 

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

Choppy camera follow. 1 Answer

How to make the camera start in the camera2 position (Unity 2D) 0 Answers

2D Camera Follow - Jittery? 1 Answer

2D Platformer Demo Camera Follow script doesn't follow player 1 Answer

2D camera follow horizantal only? 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