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 /
avatar image
0
Question by SeanSurtz · Apr 27, 2017 at 07:26 AM · cameratransformvector3camera-movementtransform.position

Move camera when mouse is near the edges of the screen

  void MoveCam()
     {
         Vector3 camPos = transform.position;
         if (Input.mousePosition.x > screenWidth - 30)
         {
             isCamMoving = true;
             camPos.x += speed * Time.deltaTime;
         }
         else if (Input.mousePosition.x < 30)
         {
             isCamMoving = true;
             camPos.x -= speed*Time.deltaTime;
         }
         
         else if (Input.mousePosition.y > screenHeight - 30)
         {
             isCamMoving = true;
             camPos.z += speed*Time.deltaTime;
         }
         else if (Input.mousePosition.y < 30)
         {
             isCamMoving = true;
             camPos.z -= speed * Time.deltaTime;
         }
         else
         {
             isCamMoving = false;
         }
     }



why isn't this code working? I'm a noob btw. I have this in the update, I tried commenting out every other part of my camera scripts except this part and it still wasn't working. What's wrong with it?

Comment
Add comment · Show 1
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 AtGfx · Apr 27, 2017 at 07:48 AM 0
Share

What do you try to do exactly ? If I read your code, it seem that when you are on the edge of your screen you move horizontally/vertically and when you are inside the edges, you also move ? And what do not work as expected ?

Something that I directly see is that your y position is never checked since your 2 first if check the x position and one of them is always true... It is the same for your last else, which is evaluated only if you Input.mousePosotion is exactly 30,30. I think you should reorganize these things according to what you try to do.

1 Reply

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

Answer by Hellium · Apr 27, 2017 at 07:27 AM

You never assign the new position to your transform :

  transform.position = camPos ;
Comment
Add comment · Show 4 · 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 AtGfx · Apr 27, 2017 at 07:49 AM -1
Share

camPos is assigned to transform.position on line 3, it should make the trick.

avatar image Hellium · Apr 27, 2017 at 07:00 PM 0
Share

No, because Vector3 are structs, not instances of classes

avatar image SeanSurtz · Apr 30, 2017 at 04:50 AM 0
Share

Whazt do you mean? What should I assign it to then?

avatar image Hellium SeanSurtz · Apr 30, 2017 at 09:45 AM 0
Share

Changing camPos does not change transform.position (Vector3 are value types, not reference types). Thus, at the end of the function, after you change the values of your camPos vector, you must assign this new vector to the real transform.position of your camera.

   void $$anonymous$$oveCam()
  {
      Vector3 camPos = transform.position;

      // $$anonymous$$ake change to camPos;

      transform.position = camPos ;
  }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera move like it does in the scene viewport? 0 Answers

Need help updating camera position to go above the player 2 Answers

Trouble converting transform.position to C# 1 Answer

How can i make my crosshair drag behind the camera? 0 Answers

How do I update values and the transform of an object on the server's and client's side? 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