Camera bounds based on texture

Hi, all,

I’m working on a camera script that will enable me to “paint” camera bounds using a grayscale gradient image. In this way, I basically am using GetPixel(x,y).grayscale (or .r, .g, .b for that matter) to drive a coefficient to my speed. The camera should slow to a halt as it reaches its “bounds,” (black on the texture) but regain speed as it moves in a different direction.

As a start, I’m using the texture to drive the XZ plane in Unity. Eventually, I’ll tackle Y, either with a second texture, or splitting it into separate channels.

I’ve been testing this for a while, but I’m running into a problem. The images I’m using are throwing an “Unsupported image format” error. It says it needs to be either a ARGB32, RGB24, or Alpha8. I know for a fact that my texture is RGB24. As a result of the error, (or in addition to it) I’m only pulling a result of 1, regardless of the position of the camera.

Here’s my code so far:

var speed = 6.0;
var xzBounds : Texture2D;
var texSize = 512;

private var moveDirection = Vector3.zero;

function FixedUpdate() {
    // Transform X and Z camera position into texture space, using the center of the image as the origin.
	curX = transform.position.x + (texSize / 2);
	curZ = transform.position.z + (texSize / 2);
    
    texSpeed = xzBounds.GetPixel(curX, curZ).grayscale;
	
    moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("UpDown"), Input.GetAxis("Vertical"));
	moveDirection = transform.TransformDirection(moveDirection);
	moveDirection *= speed;
	moveDirection *=texSpeed;
	
	//Debug message to console to check texture data
	print (texSpeed);

	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
}

@script RequireComponent(CharacterController)

I’ve also attached a JPEG and TIF of my test image.

226515–8164–$bounds_123.tif (768 KB)

Is there a line elsewhere in the code that says var texSpeed : float;

No, but I was under the impression that since it was only used in the scope of my FixedUpdate() function, I wouldn’t need it. Adding this doesn’t seem to affect it. The error remains and my output is still 1.

Which file format are you using for the image?

I’m using bounds.tif. (24-bit RGB) The odd thing is, when I pick a different texture, regardless of its bit depth, I get no change.

I’ll continue working with it and let you know what I find. Again, this may just be a result of me not knowing exactly what Unity’s looking for.