Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 DevMerlin · Jan 14, 2015 at 06:13 AM · camerapositionresolutionoffset

Is it possible to keep the player offset in place on the screen independent of resolution?

Okay, so I have a camera I've been building for a 2D sidescrolling game. The player needs to stay slightly off to the left - like an original Gradius game:

alt text

The camera moves freely from the player, and needs to stay that way. At the moment, I just can't get the offset working right - I keep trying the various screen conversions but I'm not sure what will work - here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
     public Transform TrackTarget;
     public float dampTime = 0.15f;
     public float OffsetFloat = 0.05f;
     public bool Ready = false;
     private Vector3 velocity = Vector3.zero;
     private Vector3 TargetOffset = Vector3.zero;
 
     
     void Start()
     {
         TargetOffset = (camera.WorldToViewportPoint(TrackTarget.position * OffsetFloat));
 
         Vector3 point = TrackTarget.position + TargetOffset;
         Vector3 Destination = new Vector3(point.x, point.y, transform.position.z);                
         transform.position = Destination; 
     }
     
     void FixedUpdate () {
         TargetOffset = (camera.WorldToViewportPoint(TrackTarget.position * OffsetFloat));
         
         if (Ready) {
             if (TrackTarget)
             {
                 Vector3 point = TrackTarget.position + TargetOffset;
                 Vector3 Destination = new Vector3(point.x, point.y, transform.position.z);                
                 transform.position = Vector3.SmoothDamp(transform.position, Destination, ref velocity, dampTime);
             }
         }
     }
     
     void LateUpdate()
     {
         if (Ready) {
             Vector3 CameraPosition = transform.position;
             CameraPosition.z = -10.00f;
             transform.position = CameraPosition;
         }
     }
 }
Comment
Add comment · Show 4
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 taxvi · Jan 14, 2015 at 07:54 AM 0
Share

as far as i understand you want to move the camera freely around the scene and let the ship follow it so that the relative distance from the left screen border and ship is always fixed...? if that's so I would use camera.ViewportToWorldPoint, not sure what u need to do tho...

avatar image DevMerlin · Jan 14, 2015 at 09:50 AM 0
Share

That is about where I am at the moment, unsure on the second half.

avatar image Mmmpies · Jan 14, 2015 at 10:11 AM 0
Share

This is just theory as I can't test it AT$$anonymous$$ but could you set your ships movement up and down to be 2 floats for x and y. If you press up the increase y by .01 (or whatever works for you) and the same for x. With x you might want to restrict x to between .1 and .5 with .5 being the middle of the scene.

The in update move the ship to the world point with ViewportToWorldPoint as

 Vector3 $$anonymous$$yPos = cacamera.ViewportToWorldPoint ( new Vector3 (myX, myY, camera.nearClipPlane));

Typing on a phone so sorry for typos!

avatar image DevMerlin · Jan 14, 2015 at 12:45 PM 0
Share

Huh? The vehicle the camera is following is moved with physics, and there are no issues with how that works. This is related to tracking and keeping the vehicle offset from the center of the camera, as old-school 2D shooters used to do.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Jan 14, 2015 at 10:29 AM

My two cents on this, get the boundaries of the screen on Start, then convert the positions to world position, modify the position by the ratio you want to be inside the screen, then clamp the ship x and y positions.

Here for converting to world position : http://answers.unity3d.com/questions/501893/calculating-2d-camera-bounds.html

then you create some game object that you attach to the camera so that they follow.

Finally, you clamp the ship position using those 4 objects.

Comment
Add comment · 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

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

27 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

Related Questions

Help! I want to make a following camera but in with Y position is constant ? 2 Answers

How to position the main camera in its script? 1 Answer

Can I keep a player on the left side of the screen in relation to view resolution? 0 Answers

ScreenToWorldPoint for different resolutions in 2D 1 Answer

Unity 2D Position Issues 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