#Camera zoom in script problem and learning how to implement stuff in code
1 messages · Page 1 of 1 (latest)
If you look at the docs again, you'll see that translation is the name of the parameter. It's simply a vector.
ohhhh
its a vector 3 component
So, summing it up, it moves the object by that vector that you provide it.
if the transform is a vector 3 component then why it is using another new vector 3 inside the transform
Translate(new Vector3(0, 0, dis))
That's not what it uses. It's what you provide it. You construct a new vector with (0, 0, dis) as the values.
And pass it into the function.
The function copies your vector and puts it into translation and does what it's supposed to with it.
what?
ohk
but what about the inputs i provide within the new Vector3 component
hello?
these are for constructing a new vector
if you have trouble understanding that, you can look at the Vector3 docs.
It doesn't do anything on it's own. It just stores those values. What gives it meaning is how you use it.
if you use it in rotation, you obviously interpret it as an angle. If you use it in Translate you interpret it as a position vector. Or a difference in position.
No, that's not a correct way of creating a new vector.
vector atleast take 2 values right?
in rotation and translate thing is something i still didnt understand!
Vector3 has 2 constructors as stated on the page that I linked. It either takes 2 or 3 parameters.
tea
what exactly do you not understand?
i mean where i used rotation?
Doesn't matter where. It was hypothetically speaking. If you use it as A you want it to be something matching A. If you use it as B you want it to be something matching B.
you mean rotation means matching
no, rotation is rotation
then?
A vector3 that is used for rotation is probably defined as angles in degrees
yea
A vector3 that is used for position is probably defined in unity units.
here right
float rotationAroundXAxis = direction.y * 180; // camera moves vertically
cam.transform.position = target.position;
cam.transform.Rotate(new Vector3(1, 0, 0), rotationAroundXAxis);
cam.transform.Rotate(new Vector3(0, 1, 0), rotationAroundYAxis, Space.World); // <— This is what makes it work!```
the direction is multiplied by degrees of angle
Yes and no. In this case you only use 1 component of the vector. One of it's values. The x one. Which in your case represents only the x of the direction vector between positions.
you use it as some multiplier for the angle, yeah.
i was thinking my prob was in one line only right?
yeah, I was only planning to explain about the Translate but you got carried away asking basic questions about Vectors, methods and constructors.
Shall we get back to the topic?
yea i am pretty noob with this and also new
yep
now i understand vectors
translations
and transform functions
You should really do some learning on vector math and beginner programming later.
So, going back to the topic: what was transform.Translate doing again?
yea i will
Before unity i used to work with .net
and was also using .net things
in unity only
yep
lets go back
hello?
i was awaiting for your answer
transform.translate is a vector struct
we provide it a vector component with the values we give it and it passes on the values to methods i think
transform.Translate is not a vector struct. It's a function. But anyways, I ask of what it does in the actual game not what it is. And I thought it was already clear what it is doing...😅
it will take the first values 0,0 and then takes the distance to keep the camera away from the pivot when rotated around the pivot
right?
No. I'm not asking about your code specifically right now.
I'm asking about what it does in general.
it will take values and then transform them to the game object
If you forgot, go back to the docs page and read the description again.
wait lemme
Moves the transform in the direction and distance of translation.
Okay so what it does in short?
it will transform the object in the direction and distance of translation
i mean it will transform
so i should just give it direction as well right?
No... Ok. let's go back to English lessons again. What's the verb in that sentence?
transform
it is the verb
it does the work
how is transform a verb? xD
Ok I guess it can be a verb in some situations. But in this sentence it wouldn't make any sense if that was the verb
Ok, here's a gameobject
What's the first component that it has?
ohhhhh my
never seen it
its the transform
the transform is a input
I bet you've seen it many times, since every game object has it. 😅
yea
i have never seen it
Anyways, does that make it clear, that transform is not a verb in that sentence?
yea
Then what's the verb?
transform is the inspector function
verb is the translation
moves?
It's a noun
Yes.
Now let's go back: what does transform.Translate do in simple terms?
translation is a vector 3 struct
That's not what I asked
whenever i assign it a new vector it will have 3 values
which we can add in transform
That's not what I asked
then?
what does transform.Translate do in simple terms?
go back to the docs if you've forgotten 😄
wait lemme read the docs then
it will move the transform values from translate vector
Okay, close. What I was expecting is something like It moves the object.
lol
so i am wrong
I think
You're simply adding too much meaning to it.
can you explain this pls?
In the simplest form what it does is move the object.
ohhhhk
If I ask you how print() works, I'm not asking you it's inner workings. Sure, it creates a string in the memory, passes it somewhere and does some stuff with it until eventually printing in the log and displaying on the screen. But just saying: it prints a message in the console is what it actually does.
So now that you know what Translate does, let's look at your code again.
yea
cam.transform.Translate(new Vector3(0, 0, dis));
here
it is moving the object with the values i am giving
You call it and pass it some value when the mouse button is pressed. And you're doing it in update, so every frame.
let's translate it to simple words: it moves the object by some value as long as the mouse button is pressed.
lol yea it is doing in every frame
So, if your value is say 0 0 0, it would not move the object at all, but you're passing it (0, 0, someDistance), so it will move it on the Z axis by someDistance.
yea
every frame
when the mouse button is pressed it moves the values in every frame
ohhhhhhh
so i need (0,0,0)
nope
not gonna work
it kept all the position 0
ohhhh now i get it
Let's say your distance to the object is 10. So, on the first frame, it will move the camera by 10 on the Z axis. If your camera was looking at the object, that will basically set it to the same position as the object. Then, next frame it moves by another 10 units. And then again.
it keeps updating the z axis value as distance all the time
right?
if i keep all the values
not updating, but doing something like transform.localPosition += 0, 0, distance.
all x y and z as dis
yea
it is transforming the z axis as distance only
but shouldnt it go fine without errors
?
There are no errors. It does exactly what you tell it to.
It goes the whole distance from the camera to the object in one frame.
ohhhhhhhhhh
look at this line here:
You probably tested it before writing the rest of the code, right?
yep
i did
And it worked
What's the difference between this line and the one you have at the bottom?
It is space.Self by default
What's the other difference?
I am using a new vector 3
you're using a different value
and also one that maps exactly to the distance between the camera and the object.
i am using z value
The value that you're using is bad.
the same as i zoomed in and out
then what value should it be?
In one you use scroll * 6, in the other dis.
scroll * 6 is always in the range from -6 to 6
i just need to use scroll * 6 value
Actually no, it can probably go above and lower. But anyways, it represents the value of how much your scrollwheel moved during that frame.
now what does dis represent?
yea it does
dis represents the distance
between the camera
whereas the scroll is the value we get from scroll
Yes. So you're basically zooming in by distance. Is that what you want?
and multiply it to get the speed
nope
i dont
but how to get the correct value then?
First decide what is the correct value?
i got the scroll value
i scrolled
how much do you want your camera to move to the target each frame.
according to the scroll
maybe
Then, you know what to put in there.
yeeeeee
cam.transform.Translate(new Vector3(0, 0, scroll * 6));
i tried it right now
it doesnt rotate with the zoom cuz it is used within as when the mouse is clicked
i tried to do this
cam.transform.Translate(new Vector3(0, 0, dise));
and used this
float dise = scroll * 6;
but most likely it isnt working
Share your current code
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
cam.transform.LookAt(target);
cam.transform.Translate(0, 0, scroll * 6, Space.Self);
float dise = scroll * 6;
float dis = Vector3.Distance(cam.transform.position, target.transform.position);
if (Input.GetMouseButtonDown(0))
{
previousPosition = cam.ScreenToViewportPoint(Input.mousePosition);
}
else if (Input.GetMouseButton(0))
{
Vector3 newPosition = cam.ScreenToViewportPoint(Input.mousePosition);
Vector3 direction = previousPosition - newPosition;
float rotationAroundYAxis = -direction.x * 180; // camera moves horizontally
float rotationAroundXAxis = direction.y * 180; // camera moves vertically
cam.transform.position = target.position;
cam.transform.Rotate(new Vector3(1, 0, 0), rotationAroundXAxis);
cam.transform.Rotate(new Vector3(0, 1, 0), rotationAroundYAxis, Space.World); // <— This is what makes it work!
cam.transform.Translate(new Vector3(0, 0, dise));
previousPosition = newPosition;
cam.transform.LookAt(target);
}
}```
ahhh
wait lemme
zoom is working fine
Okay. Now looking at your rotation code, it's complete bullshit. At least in your use case.
why?
can i continue it after 30 minutes
i was going to have breakfast
pls?
Because it just rotates your camera in place. Not around the object.
Because Transform.Rotate rotates the object around itself.
and it was rotating to some z axis
You should be using Transform.RotateAround if you want it to rotate around something else.
I guess cam.transform.Translate in the end was an attempt of a walkaround to that, but it's bad
yea it is
thanks to you i understood many things today
What I suggest you do is fin a tutorial that implements rotation and zoom of a camera around a point/object. I bet there are plenty of tutorials for that.