RTS game Scripting Issue

Hey, everyone. I’m having a problem with two scripts that I have. First of all i am complete noob :-P. the scripts that i am about to show are just some that i have got from tutorials from youtube and kinda combined them all into this one editing a few here in there to get it the way i like.

You will notice that one is JAVA and the other is C# which i assume might have something to do with the problem itself…

The first script here is the JAVA script, this basically is the movement of the camera, when the cursor hits the edge of the screen it moves the camera.

The second script is the Main one i use, it has everything from zoom in and out to the Selection Highlight for the units.

I really need both of these functions to work with my game, and if the first script was in C# i would try to merge it myself, but I honestly wouldn’t really know where to begin. If someone could help me Merge the two that would be perfect, or at least teach me how to make it to where they can coincide with each other without it messing up. as for now the first script doesn’t even work well.

Problem with the script: The java script will not move the camera if the main script (C#) is active, i did notice that it will move sometimes but not always. it will just scoot the screen over but it won’t go pass that.

Thanks for all of your help, i have been going around for about 10 hrs the past 2 days trying to get this figured out, but i can’t… thanks again for reading.

P.S All though i followed youtube videos to make these, i typed it all myself while following on so i could learn more, so the spacing of the code and the all out organization may not be the best…

var CamSpeed = 1.00;
var GUIsize = 25;
 
function Update () {
var recdown = Rect (0, 0, Screen.width, GUIsize);
var recup = Rect (0, Screen.height-GUIsize, Screen.width, GUIsize);
var recleft = Rect (0, 0, GUIsize, Screen.height);
var recright = Rect (Screen.width-GUIsize, 0, GUIsize, Screen.height);
 
    if (recdown.Contains(Input.mousePosition))
        transform.Translate(0, 0, -CamSpeed, Space.World);
 
    if (recup.Contains(Input.mousePosition))
        transform.Translate(0, 0, CamSpeed, Space.World);
 
    if (recleft.Contains(Input.mousePosition))
        transform.Translate(-CamSpeed, 0, 0, Space.World);
 
    if (recright.Contains(Input.mousePosition))
        transform.Translate(CamSpeed, 0, 0, Space.World);
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CameraOperator : MonoBehaviour 
{
	public Texture2D selectionHighlight = null;
	public static Rect selection = new Rect(0, 0, 0, 0);
	private Vector3 startClick = -Vector3.one;

	private static Vector3 moveToDestination = Vector3.zero;
		//Ground (walking Platform needs to be labeled "Floor" for following code)
	private static List<string> passables = new List<string>() { "Floor" };

	public float zoomMaxY = 0;
	public float zoomMinY = 0;
	public float zoomSpeed = 0.05f;
	public float zoomTime = 0.25f;
	public Vector3 zoomDestination = Vector3.zero;


	private void Update() 
	{
		CheckCamera();

		Cleanup();

		ZoomCamera ();
	}

	private void ZoomCamera()
	{
		float moveY = Input.GetAxis("Mouse ScrollWheel");

		if (moveY != 0) 
				zoomDestination = transform.position + (transform.forward * moveY) * zoomSpeed;

		if (zoomDestination != Vector3.zero  zoomDestination.y < zoomMaxY  zoomDestination.y > zoomMinY)
			{
				transform.position = Vector3.Lerp (transform.position, zoomDestination, zoomTime); 

				if (transform.position == zoomDestination)
					zoomDestination = Vector3.zero;
			}
		if (transform.position.y > zoomMaxY)
			transform.position = new Vector3 (transform.position.x, zoomMaxY, transform.position.z);
		if (transform.position.y < zoomMinY)
			transform.position = new Vector3 (transform.position.x, zoomMinY, transform.position.z);
	}

	private void CheckCamera()
	{
		if (Input.GetMouseButtonDown(0))
						startClick = Input.mousePosition;
				else if (Input.GetMouseButtonUp(0)) 
						startClick = -Vector3.one;

					if (Input.GetMouseButton(0))
					{ 
						selection = new Rect(startClick.x, InvertMouseY(startClick.y), Input.mousePosition.x - startClick.x, InvertMouseY(Input.mousePosition.y) - InvertMouseY(startClick.y));  

						if (selection.width < 0) 
						{
							selection.x += selection.width;
							selection.width = - selection.width; 
						}
						if (selection.height < 0)
						{
							selection.y += selection.height;
							selection.height = - selection.height;
             }
          }
       }
 

	private void OnGUI()
	{
		if (startClick != -Vector3.one)
		    {
		    	GUI.color = new Color(1, 1, 1, 0.5f);
			    GUI.DrawTexture(selection, selectionHighlight);
		    }
	}
  
	public static float InvertMouseY (float y)
    {
				return Screen.height - y;
	}		   
	private void Cleanup()
	{
		if (!Input.GetMouseButtonUp(1))
		moveToDestination = Vector3.zero;
	}

	public static Vector3 GetDestination()
	{
		if (moveToDestination == Vector3.zero)
		{
			RaycastHit hit;
			Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);

			if (Physics.Raycast(r,out hit))
			    {
					while (!passables.Contains(hit.transform.gameObject.name))
					{
						if (!Physics.Raycast(hit.point + r.direction * 0.1f, r.direction, out hit))
						break;
					} 
				}
				if (hit.transform != null)
					moveToDestination = hit.point;
			}

			return moveToDestination;
    }
}

Bump i really need help with this…

Just copy-pasting the contents of the JS Update function into the C# Update method should work. The only thing you’d have to add is the word ‘new’ in front of the Rect constructor calls. Nothing else is really C# specific.

I’ll also point out that GUI space and Screen space aren’t the same so if you’re checking mouse postion against some GUI area you’ll have to invert the mouse’s Y axis (Screen.height - Input.moustPosition.y). BUT - better to get the thing compiled in a single language first :slight_smile:

Do i need to remove “var” from the code? or just add new in between var and rec?

After removing var and just keeping new recdown = rect… etc… its given me a unexpected symbol error. Here is the code on what i did, again im a noob at coding, but i thank you for your help very much

	private static Vector3 moveToDestination = Vector3.zero;
		//Ground (walking Platform needs to be labeled "Floor" for following code)
	private static List<string> passables = new List<string>() { "Floor" };

	public float zoomMaxY = 0;
	public float zoomMinY = 0;
	public float zoomSpeed = 0.05f;
	public float zoomTime = 0.25f;
	public Vector3 zoomDestination = Vector3.zero;


	private void Update() 
	{
		{
			 new recdown = Rect (0, 0, Screen.width, GUIsize);
			 new recup = Rect (0, Screen.height-GUIsize, Screen.width, GUIsize);
			 new recleft = Rect (0, 0, GUIsize, Screen.height);
			 new recright = Rect (Screen.width-GUIsize, 0, GUIsize, Screen.height);

			if (recdown.Contains(Input.mousePosition))
				transform.Translate(0, 0, -CamSpeed, Space.World);
			
			if (recup.Contains(Input.mousePosition))
				transform.Translate(0, 0, CamSpeed, Space.World);
			
			if (recleft.Contains(Input.mousePosition))
				transform.Translate(-CamSpeed, 0, 0, Space.World);
			
			if (recright.Contains(Input.mousePosition))
				transform.Translate(CamSpeed, 0, 0, Space.World);
		}

		CheckCamera();

		Cleanup();

		ZoomCamera ();
	}

	private void ZoomCamera()
	{
		float moveY = Input.GetAxis("Mouse ScrollWheel");

		if (moveY != 0) 
				zoomDestination = transform.position + (transform.forward * moveY) * zoomSpeed;

		if (zoomDestination != Vector3.zero  zoomDestination.y < zoomMaxY  zoomDestination.y > zoomMinY)
			{
				transform.position = Vector3.Lerp (transform.position, zoomDestination, zoomTime); 

				if (transform.position == zoomDestination)
					zoomDestination = Vector3.zero;
			}
		if (transform.position.y > zoomMaxY)
			transform.position = new Vector3 (transform.position.x, zoomMaxY, transform.position.z);
		if (transform.position.y < zoomMinY)
			transform.position = new Vector3 (transform.position.x, zoomMinY, transform.position.z);
	}

	private void CheckCamera()
	{
		if (Input.GetMouseButtonDown(0))
						startClick = Input.mousePosition;
				else if (Input.GetMouseButtonUp(0)) 
						startClick = -Vector3.one;

					if (Input.GetMouseButton(0))
					{ 
						selection = new Rect(startClick.x, InvertMouseY(startClick.y), Input.mousePosition.x - startClick.x, InvertMouseY(Input.mousePosition.y) - InvertMouseY(startClick.y));  

						if (selection.width < 0) 
						{
							selection.x += selection.width;
							selection.width = - selection.width; 
						}
						if (selection.height < 0)
						{
							selection.y += selection.height;
							selection.height = - selection.height;
             }
          }
       }
 

	private void OnGUI()
	{
		if (startClick != -Vector3.one)
		    {
		    	GUI.color = new Color(1, 1, 1, 0.5f);
			    GUI.DrawTexture(selection, selectionHighlight);
		    }
	}
  
	public static float InvertMouseY (float y)
    {
				return Screen.height - y;
	}		   
	private void Cleanup()
	{
		if (!Input.GetMouseButtonUp(1))
		moveToDestination = Vector3.zero;
	}

	public static Vector3 GetDestination()
	{
		if (moveToDestination == Vector3.zero)
		{
			RaycastHit hit;
			Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);

			if (Physics.Raycast(r,out hit))
			    {
					while (!passables.Contains(hit.transform.gameObject.name))
					{
						if (!Physics.Raycast(hit.point + r.direction * 0.1f, r.direction, out hit))
						break;
					} 
				}
				if (hit.transform != null)
					moveToDestination = hit.point;
			}

			return moveToDestination;
    }
}

Thank you again friend!

No

var recDown = new Rect(....);

You also have extra curly brackets in your Update method now.

When you define a value in CS it looks like this:

Type varName;

In js, it will look like this:

var varName : Type;

js is a bit more forgiving, and you can either take that in a good way or bad. (depends on the person)

The word ‘new’ is required for CS, but is not completely needed in js, but can be used… observe:

// js
var rect1 = new Rect(0,0,10,10);
var rect2 = Rect(0,0,10,10);

// CS counterpart
Rect rect1 = new Rect(0,0,10,10);

So in your code:

// this:
new recdown = Rect (0, 0, Screen.width, GUIsize);

// needs to be:
Rect recdown = new Rect (0, 0, Screen.width, GUIsize);

cleaned:

		private void Update()
		{
			{
				Rect recdown = new Rect (0, 0, Screen.width, GUIsize);
				Rect recup = new Rect (0, Screen.height-GUIsize, Screen.width, GUIsize);
				Rect recleft = new Rect (0, 0, GUIsize, Screen.height);
				Rect recright = new Rect (Screen.width-GUIsize, 0, GUIsize, Screen.height);
	
				if (recdown.Contains(Input.mousePosition))
					transform.Translate(0, 0, -CamSpeed, Space.World);
			
				if (recup.Contains(Input.mousePosition))
					transform.Translate(0, 0, CamSpeed, Space.World);
			
				if (recleft.Contains(Input.mousePosition))
					transform.Translate(-CamSpeed, 0, 0, Space.World);
			
				if (recright.Contains(Input.mousePosition))
					transform.Translate(CamSpeed, 0, 0, Space.World);
			}
	
			CheckCamera();
	
			Cleanup();
	
			ZoomCamera ();
		}

var works just fine in C# as well.

Oh i see, thank you! I added the public floats to the top, now its working just as it was before, but only with 1 script! Awesome thank you.

So I took this

var recup = new Rect (0, Screen.height-GUIsize, Screen.width, GUIsize);

and replaced it with this

var recup = new Rect (0, Screen.height - Input.mousePosition.y, Screen.width, GUIsize);

was that what you were talking about? o

@bigmisterb Thanks for explaining that, it makes a lot more since now. with KelsoMRK help i got it complied all into one script, i just have to figure out how to get it to function.

I tend to modify the argument passed to Contains() but that might just be preference.

Vector3 mousePos = Input.mousePosition;
mousePos.y = Screen.height - mousePos.y;

if (someRect.Contains(mousePos))
{

}

Do you mean i need to change

if (recdown.Contains(Input.mousePosition))
		transform.Translate(0, 0, -CamSpeed, Space.World);

Into

Vector3 mousePos = Input.mousePosition;
			mousePos.y = Screen.height - mousePos.y;
			if (recdown.Contains(mousePos))
			{
				transform.Translate(0, 0, -CamSpeed, Space.World);

If so when i do that, i get a mousePos is already defined in this scope.

Make sure you don’t have some other variable named ‘mousePos’

No I don’t, i just checked all my scripts. It gives me three errors i think its because the other recs that i use have the same mousePos.

Do i need to make them numbered? like the first

recdown.contains(mousePos))

Second

recup.contains(MousePos1))

ect. or would that even work?

No, that doesn’t matter. Post your code and the exact error message.

Ok so the problem with the error was that i was putting the “Vector 3” line on every one (4 lines) that is wrong… so i changed it and now im not getting a error code. but it doesn’t move my camera when i put the mouse to the edge of the screen now.

Here is what we are editing, im not going to post all 127 lines unless you need it.

private void Update() {
		{

			 var recdown = new Rect (0, 0, Screen.width, GUIsize);
			 var recup = new Rect (0, Screen.height-GUIsize, Screen.width, GUIsize);
			 var recleft = new Rect (0, 0, GUIsize, Screen.height);
			 var recright = new Rect (Screen.width-GUIsize, 0, GUIsize, Screen.height);
		
			Vector3 mousePos = Input.mousePosition;	
			mousePos.y = Screen.height - mousePos.y;
				if (recdown.Contains(mousePos))
				{ 
					transform.Translate(0, 0, -CamSpeed, Space.World);
				}

			mousePos.y = Screen.height - mousePos.y;
				if (recup.Contains(mousePos))
				{ 
					transform.Translate(0, 0, CamSpeed, Space.World);
				}
				
			mousePos.y = Screen.height - mousePos.y;
				if (recleft.Contains(mousePos))
				{ 
					transform.Translate(-CamSpeed, 0, 0, Space.World);
				}

				
			mousePos.y = Screen.height - mousePos.y;
				if (recright.Contains(mousePos))
				{ 
					transform.Translate(CamSpeed, 0, 0, Space.World);
				}

Line 2 has a curly bracket that shouldn’t be there. Lines 16, 22, and 29 are redundant because you already did that
operation on line 9 and will actually result in a change to the Y component of the position vector every time.

I also don’t see anything here referencing screen edges since this is just checking if the cursor is inside some rectangular areas of the screen.

You might want to go do some introductory programming tutorials before attempting to progress any further.