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 /
  • Help Room /
avatar image
0
Question by Kellzaba · May 14, 2016 at 09:43 AM · movementcamera-movementpositioningmouseclicklabels

Click on different objects to move the camera

Thanks for reading this. I am very new to programming and am feeling very overwhelmed and confused by the documentation. I've tried searching the questions/answers here, but I haven't found what I need.

I am trying to code a movement system, much like in these games, where you can click in a defined area on the side of the screen and it will move the camera over to a new screen. (I have my world laid out like tiles, next to each other.)

  1. I am not sure how to check what object I'm clicking on to get the info that will tell me which way to move. I imagine that I can tag my "selection boxes" with things such as "right" "left" "up" "down" to grab as a signal in my code, but I'm not sure how to check for that.

  2. I'm not entirely sure how to them tell the camera to move a certain distance when that happens.

Here's my code (so far), though I'm sure it's all wrong. I appreciate your help and explanations!

 using UnityEngine;
 using System.Collections;
 
 public class Explorer : MonoBehaviour {
 
     // Variables
     private int moveH = 13;
     private int moveV = 12;
 
     // Use this for initialization
     void Start () {
         // Get the camera's transform info
         Transform trans = GetComponent<Transform> ();
     }
 
     // Update is called once per frame
     void Update () {
         // If clicking on selBox move camera direction
         function MoveRight(){
             if (Input.GetMouseButtonDown(0)){
                 Camera.trans.Position.X += moveH;
             }
         }
     }
 }

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
0

Answer by Tyche10 · May 14, 2016 at 10:18 AM

First of all there are some problems in your Update() function. You are declaring the MoveRight function inside the brackets of the Update function. Or you declare the MoveRight function inside your class, but outside your Update() function, and you call it in your Update by way of

 MoveRight();

(the return type should be void in C# by the way), or you just write the implementation you have right now in your MoveRight function directly into Update() like I did here. I suspect your script is on the camera, which means you already have a reference to the transform component of your camera in the Start() function in the form of your trans variable. This you can use to adjust the position of the camera in Update. However, you cannot change the x on the camera position directly. You can copy the whole Vector3 at once, but if you want to change one of the Vector3's variables you need to create a new Vector3 using the new keyword, like I did in your rewritten code. To find out what object was pressed, and to get more information about its position and other things, you can use a raycast like in this example.

 // Update is called once per frame
          void Update () {
              // If clicking on selBox move camera direction
              if (Input.GetMouseButtonDown(0)){
                      Vector3 targetPosition = new Vector3(trans.position.x += moveH, trans.position.y, trans.position.z);
                      trans.position = targetPosition;
                  }


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 Kellzaba · May 14, 2016 at 03:42 PM

Thanks for replying, @Tyche10. So I've changed my code to reflect what you've shared with me, but I'm getting seven errors on it. Here's my code: using UnityEngine; using System.Collections;

 public class Explorer : MonoBehaviour {
 
     // Variables
     private int moveH = 13;
     private int moveV = 12;
 
     // Use this for initialization
     void Start () {
         // Get the camera's transform info
         trans = GetComponent<Transform> ();
     }
 
     // Move Right Function
     void MoveRight() {
         if (Input.GetMouseButtonDown(0)){
             Vector3 targetPosition = new Vector3(trans.position.x += moveH, trans.position.y, trans.position.z);
             trans.position = targetPosition;
         }
     }
 
     // Update is called once per frame
     void Update () {
         MoveRight();
     }
 }

And here are the errors I'm getting: errors screenshot

I'm not sure what raycasting is, so I'll have to look into that. Like I said, I'm very new to this, so I'm still struggling with it.


temp.png (29.5 kB)
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 Tyche10 · May 15, 2016 at 11:14 AM 0
Share

First problem is that you never declare the trans variable. Declare it at the top so you have access to it throughout the script.

 // Variables
      private int moveH = 13;
      private int moveV = 12;
      Transform trans;

By the way, since you're using moveH and moveV to change your position values, it would be cleaner to make them floats

  // Variables
  private float moveH = 13f;
  private float moveV = 12f;
  Transform trans;

The next line should lose the equal sign in trans.position.x += moveH. Sorry I didn't catch that.

 Vector3 targetPosition = new Vector3(trans.position.x + moveH, trans.position.y, trans.position.z);

I would recommend to maybe practice some simple stuff first before starting with raycasting.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Click to Move multiplayer 0 Answers

A gameobject(cylinder) moves along with the Hand tool (top-left corner) when right clicked, even when the gameobject is not selected 0 Answers

Trying to get camera to follow mouse, and player movement to be relative to camera angle? 0 Answers

Move object to another position after Mouse Click with MoveTowards function 1 Answer

Rotate Object Axis On Swipe Cam Rotation - Android 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