• 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 j0ffe · Jun 27, 2012 at 12:56 PM · camera-movementcamera movement

Move the camera then the cursor is at the screen edge

HI! I want to make a script that moves the camera then your mouse cursor is at the end of your screen as in strategy games. Have made a script with the camera follow the mouse but the problem is it move the camera then it is on the screen and not centred and it will only move on in two directions, left and right.

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
4
Best Answer

Answer by AlucardJay · Jun 27, 2012 at 03:17 PM

Check my answer on this question :

http://answers.unity3d.com/questions/266454/top-down-2d-gamehow-to-make-the-camera-move-on-hit.html

EDIT :

the mouse inputs are X and Y, what you change from those values is your choice. For the camera, use mouse X for camera X, and mouse Y for camera Z. Attach this script to the camera :

 #pragma strict

 public var Boundary : int = 50; // distance from edge scrolling starts
 public var speed : int = 5;

 private var theScreenWidth : int;
 private var theScreenHeight : int;

 function Start() 
 {
     theScreenWidth = Screen.width;
     theScreenHeight = Screen.height;
 }

 function Update() 
 {
     if (Input.mousePosition.x > theScreenWidth - Boundary)
     {
        transform.position.x += speed * Time.deltaTime; // move on +X axis
     }

     if (Input.mousePosition.x < 0 + Boundary)
     {
        transform.position.x -= speed * Time.deltaTime; // move on -X axis
     }

     if (Input.mousePosition.y > theScreenHeight - Boundary)
     {
        transform.position.z += speed * Time.deltaTime; // move on +Z axis
     }

     if (Input.mousePosition.y < 0 + Boundary)
     {
        transform.position.z -= speed * Time.deltaTime; // move on -Z axis
     }

 }   

 function OnGUI() 
 {
     GUI.Box( Rect( (Screen.width / 2) - 140, 5, 280, 25 ), "Mouse Position = " + Input.mousePosition );
     GUI.Box( Rect( (Screen.width / 2) - 70, Screen.height - 30, 140, 25 ), "Mouse X = " + Input.mousePosition.x );
     GUI.Box( Rect( 5, (Screen.height / 2) - 12, 140, 25 ), "Mouse Y = " + Input.mousePosition.y );
 }


Comment
Add comment · Show 3 · 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 j0ffe · Jun 27, 2012 at 05:21 PM 0
Share

Thanks this solved all my problems with it except one I needed too change y to z to get the camera to move on the direction I wanted but now it's move on -z axel even if I haven't the mouse at the screen edge, what is did I wrong then I only changed the y to z?

avatar image AlucardJay · Jun 28, 2012 at 12:17 PM 0
Share

I have updated the answer. Hopefully this is what you want.

$$anonymous$$ouse Input is X and Y axis (check the numbers showing OnGUI), so use mouseX on cameraX, and mouseY on cameraZ.

avatar image j0ffe · Jun 28, 2012 at 05:26 PM 0
Share

Thanks works perfect now

avatar image
0

Answer by Flufflesthepancake · Jun 27, 2012 at 01:05 PM

Using my basic knowledge and question answering amazingness (it's pretty poor):

take the mouse position from a raycast: http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html

and then make it so that if it's a certain distance away from the edges (determine with Screen.width and Screen.height) you perform the necessary camera movement (seeing as you haven't explained how the camera moves, whether that's a rotation or a pan or a track)

You'd have to iron out things to make it pretty, but that'd be the basic idea.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera not following Player 1 Answer

How to make a camera Follow an Object moving in zigzag path? 1 Answer

camera follow gameobject moving in angular path. 0 Answers

My vector3 lerp is not working properly 1 Answer

virtual camera is destroyed after coming in and out of a scene 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