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 Frede1012 · Jul 05, 2012 at 04:52 PM · c#cameramousefollow

Camera follow mouse around the map

Hello there. So i am trying to make the camera follow the mouse. But when i did this:

 using UnityEngine;
 using System.Collections;

 public class CamMouse : MonoBehaviour {


 void Update () {
     transform.position = Input.mousePosition;
 }
 }

That worked fine with moving to the sides. But it zooms in and out when moving the mouse up and down.

So then i figured that this will work:

 using UnityEngine;
 using System.Collections;

 public class CamMouse : MonoBehaviour {


 void Update () {
     transform.position.x = Input.mousePosition.x;
 transform.position.z = Input.mousePosition.y;
 }
 }
 

But that isnt working either.

So how do i fix this?

  • Frederik

Comment
Add comment · Show 4
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 nventimiglia · Jul 05, 2012 at 05:28 PM 0
Share

What do you mean by "follow"

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

@nventimiglia what do you mean by posting these pointless comments? Are you contributing anything, or imparting any information that could be useful and informative to the asker and future readers? If you would like to know, why don't you ask a question and post some code that you have written? I am not going to be presumptious as to what the asker wants to do to modify the values from the mouse position, but if you check any of my answers, I shall do my best to answer that. Try to be informative and constructive please.

avatar image nventimiglia · Jul 05, 2012 at 06:55 PM 0
Share

Im trying to help and you rant off on me ? wtf dude ?

I had my tool tip and software cursor scripts ready to share /if/ they were applicable... Guess not.

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

maybe the tone of Uuum what about converting the mouse from screen-space first ? was mis-interpreted by me, if so I apologize. Please don't let my error affect the other users from learning and benefiting from knowledge. Your choice.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by termway · Jul 05, 2012 at 05:20 PM

Hi !

You have to create a new vector to assign the new position

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

Comment
Add comment · Show 5 · 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 nventimiglia · Jul 05, 2012 at 05:29 PM 0
Share

Uuum what about converting the mouse from screen-space first ?

avatar image termway · Jul 05, 2012 at 05:42 PM 0
Share

I think he wants to move his camera on Z-axis !

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

yep =]

Build the new Vector3 with ( mouseX , 0 , mouseY )

But it zooms in and out when moving the mouse up and down. transform.position.z = Input.mousePosition.y;

avatar image Frede1012 · Jul 05, 2012 at 05:51 PM 0
Share

This is working fine. Thanks!

avatar image AlucardJay · Jul 06, 2012 at 09:09 AM 0
Share

oh, and to keep the Y-axis the same, use :

new Vector3( Input.mousePosition.x, transform.position.y, Input.mousePosition.y );

avatar image
1

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

create a Vector3 temporary variable first, then use that to assign a value to transform.position.

 using UnityEngine;
 using System.Collections;

 public class CamMouse : MonoBehaviour {

     void Update () {
         //transform.position.x = Input.mousePosition.x;
         //transform.position.z = Input.mousePosition.y; 
         // error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. 
         // Consider storing the value in a temporary variable
 
         Vector3 thePos = new Vector3( Input.mousePosition.x, transform.position.y, Input.mousePosition.y ); // temporary variable
         transform.position = thePos;
     } 
 }
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 nventimiglia · Jul 05, 2012 at 05:29 PM 1
Share

Uuum what about converting the mouse from screen-space first ?

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

@nventimiglia Uuum, that wasn't what the person was asking =]

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

9 People are following this question.

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

C# Decrease Mouse Sensitivity Camera Move 0 Answers

Multiple Cars not working 1 Answer

Camera in fixed position which rotates with player. 0 Answers

How do I let a camera follow on one axis? 3 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