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.)
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.
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;
}
}
}
}
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;
}
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:
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.
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.