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 zerox600 · Dec 11, 2013 at 03:17 AM · camera2dmovementlimitworldtoscreenpoint

Issue with clamping player to camera view

I'm trying to keep the player within a non-moving camera's view, so that they can't take the player off screen, and have it be resolution independent.

So far, the player is limited to the view of the camera. However, if im holding the move button down, the player moves slightly past the stopping point, and when I release the button it moves back to the stopping point. If the player is at the stopping point and I press the same direction key again it will scoot over past slightly and stay there until i release the key, Then it will move back to the limit.

Here is the script controlling the players movement.

 using UnityEngine;
 using System.Collections;
 
 public class ShipMovement : MonoBehaviour {
 
     public float moveSpeed = 3.0f;        //Move speed modifier
     public float speedModifier = 1.0f;    //variable to store speed increase bonuses
     private float speedModDuration = 0;
     public float clampOffset = 12.0f;
 
     // Use this for initialization
     void Start () 
     {
 
     }
     
     // Update is called once per frame
     void Update ()
     {
         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
 
         screenPos.x = Mathf.Clamp(screenPos.x, 0f + clampOffset, Screen.width - clampOffset);
 
         transform.position = Camera.main.ScreenToWorldPoint(screenPos);
     
         if(Input.GetKey (KeyCode.D))            //Right movement 
         {
             transform.position += Vector3.right * Time.deltaTime * moveSpeed * speedModifier;
         }
 
         if(Input.GetKey(KeyCode.A))                //Left movement
         {
             transform.position += Vector3.left * Time.deltaTime * moveSpeed * speedModifier;
         }
 
         if(speedModDuration > 0)        //Decrements timer once activated
         {
             speedModDuration -= Time.deltaTime;
         }
         if(speedModDuration == 0)        //normalizes speed when time is up
         {
             speedModifier = 1.0f;
         }
         if(speedModDuration <0)            //resets timer if it drops below zero, to prevent shorted buffs
         {
             speedModDuration = 0;
         }
     }
 
     public void AdjustSpeedModifier(float modifier, float time)        //Function to adjust speed modifier
     {
         speedModifier = modifier;
         speedModDuration += time;                    //+= allows stackable durations
     }
 
 }
 
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

2 Replies

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

Answer by KellyThomas · Dec 11, 2013 at 04:01 AM

You are calling clamp at the start of your update.

This will correct the position from the last frame.

Move it to the end of the method and you have the behaviour you are expecting.

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 zerox600 · Dec 11, 2013 at 04:06 AM 0
Share

success! that worked, i've tried like 20 iterations of that script in different ways and haven't figured out what was wrong.

and to think it was that simple :\ ... thank you $$anonymous$$elly$$anonymous$$.

avatar image
0

Answer by fdmurphy123 · Dec 11, 2013 at 03:35 AM

At the end of line #9 it says = 0; It should be = 1;

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 fdmurphy123 · Dec 11, 2013 at 03:35 AM 0
Share

Or at least that is what it comes up with on $$anonymous$$e

avatar image zerox600 · Dec 11, 2013 at 03:53 AM 0
Share

line #9 is the clampOffset its set to 12.0f, im not sure what you mean by its = 0. if you mean line 8 thats a variable to store a timer for the speed multiplier. its set by the AdjustSpeed$$anonymous$$odifier() function, and takes a float as a parameter. it then subtracts time.deltatime from that every frame until it hits 0, then resets the multiplier to 1. it shouldnt have anything to do with the weird motion.

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

18 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

Related Questions

Sprite seems to lose frames when moving 0 Answers

Using Mathf.Clamp To Limit Camera Movement 0 Answers

Top down camera that follows the player and rotates with the player while remaining constrained to world bounds 0 Answers

Top down movement and rotation 2 Answers

Set limits for camera to other axis 2D 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