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
0
Question by Sam Beckham · Jan 11, 2010 at 06:23 PM · cameramouselinking

Making the Camera Follow the Mouse (Not LookAt)

Hi there.

I need a script that allows the camera to follow the mouse whilst staying focused on a point in the distance. The script I have so far is close but not really close enough.

using UnityEngine; using System.Collections;

public class MouseLookC : MonoBehaviour {

public GameObject m_Target;

// Update is called once per frame void Update() { Vector3 screenResolution = new Vector3(Screen.width, Screen.height, 0.0f); Vector3 mousePosition = Input.mousePosition;

 mousePosition.y = Screen.height - mousePosition.y; 
 mousePosition.x = Screen.width - mousePosition.x; 
 screenResolution *= 0.5f; 

 Vector3 difference = mousePosition - screenResolution; 
 transform.Translate(difference * Time.smoothDeltaTime * -0.05f); 
 transform.LookAt(m_Target.transform); 

 Vector3 newPosition = transform.position; 
 newPosition.z = 50f; 
 transform.position = newPosition; 

} }

The above script calculates the distance that the mouse is from the central point and uses that value to move the camera. All this script does is move the camera faster, the further the mouse is from the centre point but I want the camera to stick to the mouse a bit like this swf I created. Here

Please help as I am tearing my hair out here :(

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 andeeee · Jan 11, 2010 at 08:48 PM

For left/right, subtract the screen's centre position from the current mouse position. (This will give you a negative value if the mouse if left of centre and positive if right, etc). Then, divide the resulting value by half of the screen's width - you will get a value that varies between -1 and +1 as you move the mouse from extreme left to extreme right. You can then multiply this value by the maximum angle you want to turn. You can use Quaternion.Euler to perform the actual rotation of the object by this angle. You can use the same basic idea for up/down rotation. To simplify things, it is often better to have your camera inside a parent object and rotate the parent for left/right motion, but rotate the camera object for up/down.

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
avatar image
0

Answer by Muuskii · Jul 22, 2012 at 09:13 PM

Here is a neutered version of what I use:

 void Start () 
 {
 cameraVelocity = Vector3.zero;
 zoomVelocity = 0.0f;
 zoomTarget = height;
  
 targetExtents = target.renderer.bounds.extents.magnitude * 0.75f;    //The game object I designed this to work with needed the * 0.75 your results may vary
 }
 
 
 void update()
 {
 
 float centerX = Screen.width * 0.5f;      
 float centerY = Screen.height * 0.5f;
  
 float offsetX = Input.mousePosition.x - centerX;    //x = 0 is left of screen 
 float offsetY = Input.mousePosition.y - centerY;   //y = 0 is bottom of screen
  
 offsetX = offsetX / centerX;   //these become -1 < offset < +1
 offsetY = offsetY / centerY;
     
 offsetX = Mathf.Clamp(offsetX, -1, 1); 
 offsetY = Mathf.Clamp(offsetY, -1, 1);
  
 //height * tan(field of view) puts target at the edge of the screen, minus the tan of how big the target is at this distance keeps it's size in view
 
 float maxOffsetMagnitude = height * (Mathf.Tan( ( camera.fieldOfView * Mathf.Deg2Rad) * 0.5f ) - targetExtents/height);   
  
 float aspectRatio = (float)Screen.width / (float)Screen.height;           //Warning, dividing ints, the cast is required
 targetOffset = new Vector3(offsetX * aspectRatio, 0, offsetY) * maxOffsetMagnitude;
 
 Offset = Vector3.SmoothDamp(Offset, targetOffset, ref cameraVelocity, smoothTime);
 
 transform.position = Offset + target.position + target.up * height; //set camera position
 
 }


It works with the camera facing straight down which imo is more comfortable.

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.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

rotate camera on mouse reaching edges 1 Answer

simple camera script 0 Answers

Camera movement independent of player control 1 Answer

First Person Controller - Mouse look doesn't work 1 Answer

Move the camera when the mouse reaches the edges of the screen 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