I want my camera to only move in X and Z (but to follow the mouse, so mouse goes up cam goes up, mouse goes down cam goes down…), without rotation, this is my script, what am I doing wrong ???
var mousePosition : Vector3;
var smooth = 2.0;
var tiltAngle = 30.0;
function Update () {
var target = Quaternion.Euler (75, 0, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);;
transform.position = mousePosition;
}
ps: the prob is, i want the camera to look from top facing downards for ex: a cube, and theres no rotations but only the cam can move X and Z axis (by mouse going up or down or left or right) ?
I only want he camera to move x z axis, o the mouse goes up cam up left etc…
any help here?
alright whenever people feel like helping out -_-
here’s another problem, build is just a cube,
if (Input.GetButtonDown ("Fire1")) {
// Construct a ray from the current mouse coordinates
var hit = RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray)) {
// Create a particle if hit
Instantiate (build, transform.position, transform.rotation);
}
}
it doesnt add the cube on the position where its hit, any help there?
You’ve got an extra semicolon in your script. Also, you are in fact rotating the object, which seems to conflict with your statement that the camera shouldn’t rotate (?).
Can you explain what the problem is exactly?
ignore the 2nd problem its fixed, its about the first , the camera.
So what’s the problem exactly?
alright here is the problem, most of it its fixed, now the only thing I’m missing is ,
how do i detect that the mouse is almost on the top of the screen, on the left side, right side and bottom,
since I want to make the camera move only after its close to the top, etc,
here’s my code so far:
var speed : float = 10.0;
var Speed : float = 2.0;
function Update () {
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * speed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (rotation, 0, translation);
// Get the mouse delta. This is not in the range -1...1
var h : float = Speed * Input.GetAxis ("Mouse X");
var v : float = Speed * Input.GetAxis ("Mouse Y");
transform.Translate (h, 0, v);
}
basically i guess i will need some if (ontheedge) { transform.Translate (h, 0, v); }
but how do I know that the mouse is on the edge of the screen (top, right, left bottom)
that’s what i’m trying to figure out now, and than when its there, well the rest is made ?? any help here?
Screen coordinates go from 0 to Screen.width horizontally and 0 to Screen.height vertically. The first step would be to define a ‘screen edge buffer’ amount, which could be, say, a percentage of the screen height (e.g. 10%) converted to pixel units, e.g.:
float edgeBuffer = Screen.height * edgeBufferPercent;
Then, if the x component of the mouse position is less than the edge buffer, you move the camera left; if the x component is greater than ‘Screen.width - edgeBuffer’, you move the camera right; and similarly with up and down.
i tried playing around with the information you gave me, but I still can’t figure it out, i need a little bit more help with it, what do i put edgeBufferPercent = to, and so ?
and after that do i say if (v > edgeBuffer) {
transform.Translate (h, 0, v);
}
or ?
‘edgeBufferPercent’ would most likely be a public variable (so you can set its value in the inspector). ‘edgeBuffer’ can be created wherever (for example, it could be a member variable whose value you would set in Start() or Awake(), or it could be a local variable that you just create when you need it).
As for your code example, it’s sort of close - is ‘v’ supposed to be the y coordinate of the mouse position?
In any case, think about what the conditional ‘v > edgeBuffer’ implies. Say the edge buffer is 20 pixels; with your code, the camera would move anytime the y position of the mouse coordinate is greater than 20. Is that the desired behavior? (To give you a hint, in my previous post I was fairly specific about how you’d want to set up the conditionals.)
As for moving the camera, presumably you’d want to move it in the horizontal direction when the mouse is in a ‘horizontal buffer region’, and move it in the vertical direction when the mouse is in a ‘vertical buffer region’. If the mouse is in a ‘corner region’ (where a vertical and horizontal buffer region overlap), you’d want to move the camera in both the horizontal and vertical directions.
Also, when moving the camera, you’ll most likely want to scale the offset by the current time step.
You’re on the right track with your current code, but some adjustments are needed. What I’d recommend is to start with some simple code that determines whether the mouse is in a horizontal and/or vertical buffer region (using the conditionals I described in my previous post) and then print a message to the console indicating which region(s) the mouse is in. One you’ve got that working, you can move on to moving the camera accordingly.
I understand what you mean but i can’t put it into code,
this is the best thing i came up with:
var speed : float = 10.0;
var Speed : float = 2.0;
var edgeBufferPercent = 3;
function Update () {
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * speed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (rotation, 0, translation);
// Get the mouse delta. This is not in the range -1...1
var h : float = Speed * Input.GetAxis ("Mouse X");
var v : float = Speed * Input.GetAxis ("Mouse Y");
var edgeBuffer : float = Screen.height * edgeBufferPercent;
if (Input.mousePosition > Screen.width - edgeBuffer) {
transform.Translate (h, 0, v);
}
}
if you could write out the code for only ex: left side of the screen and the cam to move, i can study it and learn from it and do the rest…
What’s this code:
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * speed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (rotation, 0, translation);
Intended to do? It looks to me like you’re translating the camera twice: once in response to input from the ‘Horizontal’ and ‘Vertical’ axes, and once in response to the mouse moving to the edge of the screen. Is that the desired behavior? Also, why is your ‘x’ translation called ‘rotation’?
Yes sorry its so unclear, the first time its for the camera to move with the arrow keys, and the second time is with the mouse, which is where i’m having problems , well I got it to move but I need it to move only when its on the edge of the screen, I’m simply new to this and im studying other peoples examples and putting my own down together, here is what I’ve got so far,
var speed : float = 10.0;
var Speed : float = 2.0;
var edgeBufferPercent = 3;
var minMaxZoomHeight : Vector2;
var mouseWheelSpeed : float = 1500.0;
function Update () {
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * speed;
translation *= Time.deltaTime;
transform.Translate (translation, 0, translation);
// Get the mouse delta. This is not in the range -1...1
var h : float = Speed * Input.GetAxis ("Mouse X");
var v : float = Speed * Input.GetAxis ("Mouse Y");
// var edgeBuffer : float = Screen.height * edgeBufferPercent;
// if (Input.mousePosition > Screen.width - edgeBuffer) {
// transform.Translate (h, 0, v);
// }
// from here on its the mouse wheel zoom in and out
var mouseWheel : float = Input.GetAxis ("Mouse ScrollWheel");
if (mouseWheel != 0)
{
var currentHeight : float = transform.position.y;
// change the height of the cam according to the movement of the mouse wheel
currentHeight -= mouseWheel * mouseWheelSpeed * Time.deltaTime;
// Set (and clamp) min/max height values
currentHeight = Mathf.Clamp(currentHeight, minMaxZoomHeight.x, minMaxZoomHeight.y);
transform.position = Vector3(transform.position.x, currentHeight, transform.position.z);
}
}
the only thing i’m missing is the edge of the mouse and to make it work
thats the hardest part so far
Ok, here’s some pseudocode to help get you pointed in the right direction (untested):
float edgeBuffer = Screen.height * edgeBufferPercent;
float x = Input.mousePosition.x;
float y = Input.mousePosition.y;
Vector2 delta = Vector2.zero;
if (x < edgeBuffer) {
delta.x = -1;
} else if (x > Screen.width - edgeBuffer) {
delta.x = 1;
}
if (y < edgeBuffer) {
delta.y = -1;
} else if (y > Screen.height - edgeBuffer) {
delta.y = 1;
}
// I don't know how your camera is set up, but this assumes
// that the world x axis is horizontal relative to the screen, and
// the world z axis is vertical:
transform.Translate(
delta.x * moveSpeed * Time.deltaTime,
0,
delta.y * moveSpeed * Time.deltaTime
);
what should i put my edge buffer percent = ?
also, here is how i set it up
var edgeBuffer : float = Screen.height * edgeBufferPercent;
var mx : float = Input.mousePosition.x;
var my : float = Input.mousePosition.y;
var delta = Vector2.zero;
if (mx < edgeBuffer) {
delta.x = -1;
} else if (mx > Screen.width - edgeBuffer) {
delta.x = 1;
}
if (my < edgeBuffer) {
delta.y = -1;
} else if (my > Screen.height - edgeBuffer) {
delta.y = 1;
}
transform.Translate(delta.x * Speed * Time.deltaTime, 0, delta.y * Speed * Time.deltaTime);
i’m guessing it still needs alot of work ?
but thanks for the help, this should be good enough to make it work, i’l play around with it, if anyone needs this script let me know i’l post the finished version.
Whatever you want. It should be greater than 0 and less than .5, and smaller values will mean you have to move the mouse closer to the edge of the screen before the camera will start moving. I’d probably start with, say, .1 and adjust from there.
I don’t know - is it working? What you have there looks right to me, but again, the solution I posted is off the top of my head (I haven’t actually tested it).
unfortunately it doesnt work, this is the script i had to change
var edgeBuffer : float = Screen.height * edgeBufferPercent;
var mx : float = Input.mousePosition.x;
var my : float = Input.mousePosition.y;
var delta = Vector2.zero;
if (mx < edgeBuffer) {
delta.x = -1;
} else if (mx > Screen.width - edgeBuffer) {
delta.x = 1;
}
if (my < edgeBuffer) {
delta.y = -1;
} else if (my > Screen.height - edgeBuffer) {
delta.y = 1;
}
transform.Translate(delta.x * Speed * Time.deltaTime, 0, delta.y * Speed * Time.deltaTime);
to make it work in unity, and now the screen goes automatically downwards left, and if i move the mouse anywhere it has no reaction except if i move it to the right of the screen, and it keeps going.
I tried playing with it even making it vector3, and so, i still have no luck with getting it work 
Use the debugger or add some debug output to find out what is happening in your code.
I’m new to this, so what kind of debugger should i put?
With Unity 3.x you can use an actual debugger, but in lieu of that, a very simple, low-tech way to debug your code is to output information to the console as the code is running, e.g. using Debug.Log(). For example, the following statement:
Debug.Log("Hello");
Will print ‘Hello’ to the console. You can output types other than strings as well, such as floats, vectors, etc. (basically any type that implements ToString(), AFAIK at least).
Based on your description, it appears your code is not doing what you want it to. ‘Debugging’ is basically the process of determining why your code isn’t doing what you want it to. By adding console output, you can find things out about what your code is doing and (one hopes) home in on the cause of whatever problems you’re seeing.
There are lots of things you could print out, so I’m not going to give you a point-by-point rundown of everything you could possibly do to debug your code. But, a good place to start might be to print out the value of ‘delta’. If ‘edgeBufferPercent’ is set to a reasonable value and the mouse is more or less in the middle of the screen, but ‘delta’ is something other than (0, 0), then that will give you a hint as to the problem. From there, you could print out mx, my, edgeBuffer, Screen.width, and Screen.height to see if they’re more or less what you expect them to be. You could also add print statements in the blocks of code associated with the conditionals to see which conditionals are evaluating to true. And so on.
Does that makes sense?