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 Datester35 · Dec 31, 2013 at 03:04 PM · gamecubertspan

How can you make a mouse pan in game?

I have been following a tutorial online about how to make an rts strategy game. In my scene, there are 3 object; A terrain called TerrainMain, the main camera, and a cube called target. Here is the code of my non-functioning c# script.

 using UnityEngine;
 using System.Collections;
 
 public class MousePoint : MonoBehaviour {
 
     RaycastHit hit;
     
     private float raycastLength = 400;
     void Update()
     {
         GameObject Target = GameObject.Find ("Target");
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if(Physics.Raycast (ray, out hit, raycastLength)){
             Debug.Log (hit.collider.name);
             if (hit.collider.name == "TerrainMain")
             {
                 Target.transform.position = hit.point;
             }
         }
 
         Debug.DrawRay(ray.origin, ray.direction*raycastLength, Color.yellow);
 
     }
 }



I can get the cube to move around the terrain with my mouse, but I can't actually pan in the x and z directions.

Comment
Add comment · Show 2
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 Xarathorn · Dec 31, 2013 at 05:05 PM 0
Share

Do you mean you want the camera to pan around according to the mouses position or that you want the mouse to move around on the screen?

avatar image Datester35 · Dec 31, 2013 at 06:50 PM 0
Share

Yeah, sorry I mistyped it. I want the camera to move to the left when the mouse was at the left of the screen, the right when the mouse was at the right (etc.)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by blueteak · Dec 31, 2013 at 06:14 PM

You need a fair bit more code to get the camera to pan:

First - You want a if(Input.GetMouseButton(0)) to trigger the panning if LMB is pressed. This will go under the if(hit.collider.name == "TerrainMain")

Second - Once you have checked that you are clicking on the terrain, you need to move the camera when you move the mouse while clicked.

I'm not sure the best way to do this, but I would try

 transform.Translate(new Vector3( Input.GetAxis("Mouse X"), 0 , Input.GetAxis("Mouse Y")));

within that last if( button pressed ). (This assumes that the script is attached to the camera, otherwise use Camera.main.transform.Translate(...);

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

20 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

Related Questions

RTS Camera: Rotate Around Point but then Pan Directions Flip 1 Answer

I need help for my RTS Games! 0 Answers

GUI.DrawTexture doesn't show in OnGUI() call 0 Answers

I need help with my GUI text for my Real Time Strategy Game 0 Answers

How do i make a hollow object? 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