• 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
Question by Frede1012 · Jul 05, 2012 at 06:00 PM · c#camerascreenmove

Camera move when reach edge of screen

Hello there.

I am trying to make the camera move when it reaches the edge of the screen.

I have this:

 using UnityEngine;
 using System.Collections;

 public class CamMouse : MonoBehaviour
 {
 public void Update()
 {
     transform.position = new Vector3(
           Input.mousePosition.x,
           0,
           Input.mousePosition.y);

 }

 }

So how do i make it only move when it reaches the edge of the screen. And not just follow the mouse. So if the mouse reaches the edge to the right the camera will go right.

Also i have a problem with that when i start playing the game, the y position of the camera sets to 0??

  • Frederik

Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by AlucardJay · Jul 05, 2012 at 06:50 PM

Check my answer to the same question here :

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

and here :

http://answers.unity3d.com/questions/275436/move-the-camera-then-the-cursor-is-at-the-screen-e.html

Comment
clunk47

People who like this

1 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 Frede1012 · Jul 05, 2012 at 06:57 PM 0
Share

Thank you very much!

avatar image AlucardJay · Jul 05, 2012 at 06:59 PM 0
Share

no worries =]

avatar image

Answer by Jake Haas · Jul 05, 2012 at 08:39 PM

This is what I use in my RTS game:

var edgeDist : float = 50.0; //pixels var moveSpeed : float = 10.0; var rotateSpeed : float = 20.0; var height : float = 25.0; var heightChangeSpeed : float = .1;

function Update () { var hit : RaycastHit; if (Physics.Raycast(transform.position, transform.forward, hit, Mathf.Infinity, Layers.CreateInclusiveMask([Layers.scape]))) { transform.position = Vector3.Lerp(transform.position, hit.point - transform.forward*height*(Input.GetKey("z") ? 4.0 : 1.0), 0.1); } transform.RotateAround(hit.point, Vector3.up, Input.GetAxis("Camera Rotate")*rotateSpeed*Time.deltaTime);

 height = height * (-Input.GetAxis("Mouse ScrollWheel")*heightChangeSpeed + 1);
 
 var move : Vector3 = Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0);
 if (Input.mousePosition.x < edgeDist) move.x=-1;
 if (Input.mousePosition.x > Screen.width- edgeDist) move.x=1;
 if (Input.mousePosition.y < edgeDist) move.y=-1;
 if (Input.mousePosition.y > Screen.height- edgeDist) move.y=1;
 move *= moveSpeed * height;
 
 move = transform.rotation * move;
 move.y=0;
 transform.position += move * Time.deltaTime;
 

Comment
SisterKy

People who like this

1 Show 0 · 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

Answer by _EdzUp_ · Jul 05, 2012 at 06:27 PM

why not check the mouse x position and do something like:


if ( Input.mouseposition.x <20 ) {
   //rotate camera left here
}
if ( Input.mouseposition.x >Screen.width -20 ) {
   //rotate camera right here
}

the 20 is the number of pixels that will be at the sides and the whole section of code should be in the function Update()... function call :)

You can move the camera where you want in the Inspector or in code if you have a variable:


var Cam:Camera;

then attach your camera in the inspector to the variable Cam then in code you can change anything about it with commands like:


Cam.transform.position = Vector3( -50, 50, 20 ); //this will move it to X:-50, Y:50 Z:20

:D

Comment

People who like this

0 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 Frede1012 · Jul 05, 2012 at 06:32 PM 0
Share

Im not trying to rotate. Just move the camera when it reaches the edge of the screen.

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

8 People are following this question.

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

Related Questions

C# move main camra via mouse position on boarders around screen script for RTS Game 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Disabling a child script within the parent. 0 Answers

C# - How to smoothly & slightly move camera while player moves? 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