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 JBobo_lee1231 · Aug 12, 2020 at 12:31 AM · 2d gamecamera-movementtransform.positioncamera follow

How can you move a camera a set amount of units based on the player's x position in a 2d game?

Hi, I am still new to unity so sorry is this sounds like a stupid question.

I am trying to make a 2d game where if the player is inside the camera, the camera is still, however once the player moves to the edge of the camera, the camera move a set amount of units towards the edge that the player is at. However, I am currently encountering some errors. The script that I made goes as follows:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CameraMovement : MonoBehaviour {

 public GameObject Player;
 public GameObject Camera;
 //Half camera size is 12 units
 public int Halfcamera = 12;
 //how much the camera moves
 public int CameraMove = 22;
 
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     if (Player.tranform.position.x > (Camera.transform.position.x + Halfcamera))
     {
         Camera + CameraMove;
     }
     else if (Player.transform.position.x < (Camera.transform.position.x - Halfcamera))
     {
         Camera - CameraMove;
     }
 }

}

If there is something wrong that I am doing then please inform me, any help is appreciated.,

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 Ymrasu · Aug 12, 2020 at 07:20 AM

You should be careful in what you name your variables, Camera is already used by a Class; The convention is that variable names start with a lowercase and Class names start with Uppercase. Another error you are getting is that you can not add the camera (GameObject) and a number (int) together; You need to access the position of the camera through it's transform (transform.position) and convert the number into a Vector3 to match the position's type.


The camera can be access through it's class (Camera.main) instead of making a variable for it. The camera can convert the player's world position into something easier to manage with the camera, in this case we can convert it to a viewport point (WorldToViewportPoint()). You can think of the viewport in terms of percentage of the screen (0 to 1); so if the converted player's position is less than 0 or if it is greater than 1, he is off screen.


Here is a way you could do it:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraMovement : MonoBehaviour
 {
     public GameObject player;
     public int cameraMoveAmount = 22;
 
     void Update()
     {
         // convert player position to a viewport point
         var playerViewportPosition = Camera.main.WorldToViewportPoint(player.transform.position);
 
         // is the player offscreen to the right?
         if (playerViewportPosition.x > 1f) {
             Camera.main.transform.position += Vector3.right * cameraMoveAmount; // move right by move amount
         }
         // is the player offscreen to the left?
         else if (playerViewportPosition.x < 0f) {
             Camera.main.transform.position += Vector3.left * cameraMoveAmount; // move left by move amount
         }
 
     }
 }


Comment
Add comment · Show 1 · 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 JBobo_lee1231 · Aug 12, 2020 at 09:12 PM 0
Share

Thanks! This helped me a lot with my camera problems.

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

178 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

camera script not working 1 Answer

How to make the camera acquire the position of a gameobject and start following it? 1 Answer

My Player doesn't move anymore. I didn't change a thing in the script and it did work before. What's wrong? 1 Answer

Strange thing happening 1 Answer

Smooth camera script looking at left hand side of character. 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