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 /
This question was closed Apr 23, 2016 at 10:23 PM by StigC for the following reason:

Didn't explain myself well enough, have asked a different but related question since then.

avatar image
1
Question by StigC · Apr 19, 2016 at 09:27 PM · camerapositioncursorfollowboundary

Camera Follow Mouse

Hi there

I've got this piece of code for my camera (thanks to @simco500) to look around the player, according to the mouse position. It works like a charm, except I can't adjust how far the camera can travel. Simpy put, I'd like to give it boundaries/tell it how far out it can go. Could anyone help me out?

 #pragma strict
 
  var Damping = 6.0;
  var Player : Transform;
  var Height : float = 13.0;
  var Offset : float = 0.0;
  
  private var Center : Vector3;
  
  function Update () 
  {
      var mousePos = Input.mousePosition;
      var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
      
      var PlayerPosition = Player.position;
      
      Center = Vector2((PlayerPosition.x + CursorPosition.x) / 2, (PlayerPosition.y + CursorPosition.y) / 2);
      
      transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping);
  }

Here's a shot of just how way too far it travels.

Shotofhowfaritcantravel

Cheers!

screen-shot-2016-04-19-at-222040.png (33.6 kB)
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

  • Sort: 
avatar image
0

Answer by miahmiah · Apr 19, 2016 at 11:22 PM

I would change this part:

 Center = Vector2((PlayerPosition.x + CursorPosition.x) / 2, (PlayerPosition.y + CursorPosition.y) / 2);

Add these definitions:

 var changex : float = 0.0;
 var changey : float = 0.0;
 var limitx : int = 100;
 var limity : int = 100;

And make it like this:

 changex = (PlayerPosition.x + CursorPosition.x) / 2;
 changey = (PlayerPosition.y + CursorPosition.y) / 2;
 if (changex > limitx) { changex = limitx; }
 if (changey > limity) { changey = limity; }
 Center = Vector2(changex, changey);
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 StigC · Apr 20, 2016 at 07:00 AM 0
Share

Cheers, that almost fixes it!

Only problem is it's not affecting $$anonymous$$us values. $$anonymous$$InusValues Camera can still freely move to left and down, up and right is getting restricted though.

Thanks!

screen-shot-2016-04-20-at-075642.png (22.1 kB)
avatar image
0

Answer by incorrect · Apr 19, 2016 at 10:22 PM

Just use Mathf.Clamp to limit your center's position.

 float horizontalLimit = 1f;
 
 float verticalLimit = 1f;
 
 Center.x = Mathf.Clamp(Center.x, -horizontalLimit, horizontalLimit);
 Center.y = Mathf.Clamp(Center.y, -verticalLimit, verticalLimit);


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 StigC · Apr 19, 2016 at 10:47 PM 0
Share

Like this?

 #pragma strict
 
  var Damping = 6.0;
  var Player : Transform;
  var Height : float = 13.0;
  var Offset : float = 0.0;
  var horizontalLimit = 1000f;
  var verticalLimit = 1000f;
 
  private var Center : Vector3;
 
  
  function Update () 
  {
      var mousePos = Input.mousePosition;
      var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
      
      var PlayerPosition = Player.position;
 
      Center.x = $$anonymous$$athf.Clamp(Center.x, -horizontalLimit, horizontalLimit);
      Center.y = $$anonymous$$athf.Clamp(Center.y, -verticalLimit, verticalLimit);
 
      transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping);
  }
 

Doesn't work for me, camera stopped moving

avatar image incorrect StigC · Apr 20, 2016 at 08:26 AM 0
Share

I gave you the idea how to limit camera movement. But you still have to understand what your code is doing. It stopped moving because you completely removed center point calculation, so now you are clamping zero vector.

Follow this Question

Answers Answers and Comments

51 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 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

Camera rotation around player while following. 6 Answers

How do I let a camera follow on one axis? 3 Answers

how to stop the camera to follow the player on his jump movement ? 2 Answers

Camera position not updating 1 Answer

Having a 3D text appear in the middle of the screen without using GUI (C#) 2 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