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 uberokeer · Jul 07, 2013 at 04:25 AM · controlscamera-lookenvironment

Using Unity like controls in-game

Hello, what i'm trying to do is create a RTS game and i need very specific camera controls. So what i need to do is create controls like in the Unity environment. So when i right-click i can rotate the camera towards my mouse, like in Unity. And i'm not quite sure where to start. I'm guessing it would go something like this:

 var : mouseInput = Input.mousePosition;
 function Update(){
 if (Input.GetAxis("Jump")){
 Camera.main.transform.lookat(mouseInput)
 }
 }

Or something like that... I'm not sure exactly how i would go about doing this. That's why i'm asking this question. Thanks if you can answer this for me! :D One more thing, i suck when it comes to ray cast. So i was wondering if there was some way to do this without using it. Thanks!! :D

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Rydrako · Jul 07, 2013 at 05:09 AM

There is a script called MouseLook in the Character Controller Asset Package (To get it go to Asset (on top bar) > Import Package > Character Controller) that gives that lets you look around just like that. Attach it to your camera then attach a new script that will enable and disable the MouseLook Script like this:

 function Update ()
 {
           if(Input.GetButton("Fire2"))//right-click button
           {
                     GetComponent("MouseLook").enabled = true;
           }
           else
           {
                     GetComponent("MouseLook").enabled = false;
           }
 }

And that should work for you!

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 SinisterRainbow · Jul 07, 2013 at 05:14 AM

Answering your more specific question if you want to look at a mouse position you need to transform the mouseinput from screenspace to a worldspace vector with Camera.main.ViewportToWorldPoint(...) Note, you will have to set the depth (z) by yourself though this way. if you are working with a terrain, you may want to cast a ray out and test for a terrain hit to get that position vector instead.

Then you can use a rotateTowards function on the camera (in a coroutine for example to smooth it). The specific code to do this depends on what you are doing, and is really your job once you understand the basics.

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 robertbu · Jul 07, 2013 at 06:33 AM

If you are looking for something that behaves similar to the way the right mouse button works in Scene view, put this on the camera:

 #pragma strict
 
 private var v3Mouse : Vector3;
 private var v3Center = Vector3(Screen.width / 2.0, Screen.height / 2.0, 0.0);
  
 function Update () {
     if (Input.GetMouseButtonDown(1)) {
         v3Mouse = Input.mousePosition;
     }
     
     if (Input.GetMouseButton(1)) {
         var v3 = Input.mousePosition - v3Mouse;
         var ray = Camera.main.ScreenPointToRay(v3Center + v3);
         transform.rotation = Quaternion.LookRotation(ray.direction);
         v3Mouse = Input.mousePosition;
     }
 } 

Oh, and their is no Raycasts()!

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

16 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

Related Questions

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

open a link inside unity 2 Answers

Unity is acting REALLY weird 0 Answers

Adding a SDK to my game 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