- Home /
C# move main camra via mouse position on boarders around screen script for RTS Game
So I have been working on this for a few hours now. I know I'm new but i'm trying to learn what I'm doing wrong I have seen alot of different scripts on the forums but they don't seem to do what I want. The idea is that as the gamer will move the mouse to the edge of any four sides of the screen and the if statment will then move the camera when it detects the movement. Each update it is suppose to scroll faster. When the user moves the coursor off the edge of the screen it should stop the movement of the camera but doesn't. I have tried to implement an else if and else statement but it seems to ignore those. Code shown below. Thanks!
using UnityEngine;
using System.Collections;
public class cameraLook : MonoBehaviour {
public float speed = 10.0f; //velocity at which the screen should move, won't work right now becuase movement.x,y is set to 0 at end of the code
public Vector3 movement = Vector3.zero;
public float maxScreenWidth = Screen.width - 20; //code that identifes max screen width for conditional statement
public float maxScreenHeight = Screen.height - 20; // code that identifies max screen height " " "
public float numberOfPixlesToMove = 1;
// Use this for initialization
void Start () {
Debug.Log ("Can this run!!!");
}
// Update is called once per frame
void LateUpdate () {
if (Input.mousePosition.x <= 20) //moves screen to the left
{
movement.x = movement.x-numberOfPixlesToMove;
}
if (Input.mousePosition.x >= maxScreenWidth) // moves screen to the right
{
movement.x = movement.x+numberOfPixlesToMove;
}
if (Input.mousePosition.y <= 20)// moves screen up
{
movement.y = movement.y-numberOfPixlesToMove;
}
if (Input.mousePosition.y >= maxScreenHeight)// moves screen down
{
movement.y = movement.y+numberOfPixlesToMove;
}
//else if(movement.x = 0, movement.y = 0);
/*
else if (Input.mousePosition.x >= 20 && Input.mousePosition.x <= maxScreenWidth || Input.mousePosition.y >= 20 && Input.mousePosition.y <= maxScreenWidth)
{
movement.x = 0;
movement.y = 0;
}
*/
/*
if (Input.mousePosition.y >= 20 || Input.mousePosition.y <= maxScreenWidth)
{
movement.y = 0;
}
*/
/*
else
{
movement.x = 0;
movement.y = 0;
}
*/
transform.Translate (movement * speed * Time.deltaTime, Space.Self);
// movement.x = 0; // if this line and the line below are commented out it will have some velocity with movement
// movement.y = 0;
// return;
}
}
Your answer
Follow this Question
Related Questions
Camera move when reach edge of screen 3 Answers
Camera follow mouse around the map 2 Answers
C# Decrease Mouse Sensitivity Camera Move 0 Answers
Moving two objects and avoiding overlap with each other 1 Answer
Distribute terrain in zones 3 Answers