#Camera zoom in script problem and learning how to implement stuff in code

1 messages · Page 1 of 1 (latest)

inner spear
pure trout
#

yes now i know it

#

but what to do next?

inner spear
#

If you look at the docs again, you'll see that translation is the name of the parameter. It's simply a vector.

inner spear
#

So, summing it up, it moves the object by that vector that you provide it.

pure trout
#

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))

inner spear
#

And pass it into the function.

pure trout
#

and 0,0 is the distance

inner spear
#

The function copies your vector and puts it into translation and does what it's supposed to with it.

inner spear
pure trout
#

but what about the inputs i provide within the new Vector3 component

#

hello?

inner spear
#

these are for constructing a new vector

#

if you have trouble understanding that, you can look at the Vector3 docs.

pure trout
#

i got it

pure trout
#

vextor 3 is a component which takes 3 inputs right?

#

distance , position , speed

inner spear
#

It's not a component

#

it's a struct.

pure trout
#

ohk

#

lemme read the doc

inner spear
#

But otherwise yes. It's basically 3 float values

#

what they define is up to you.

pure trout
#

yea

#

its 3 float value

#

and it does what i define to it

inner spear
#

It doesn't do anything on it's own. It just stores those values. What gives it meaning is how you use it.

pure trout
#

Translate(new Vector3(dis))

#

right?

inner spear
#

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.

inner spear
pure trout
pure trout
inner spear
#

Vector3 has 2 constructors as stated on the page that I linked. It either takes 2 or 3 parameters.

inner spear
pure trout
inner spear
#

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.

pure trout
inner spear
#

no, rotation is rotation

pure trout
#

then?

inner spear
#

A vector3 that is used for rotation is probably defined as angles in degrees

inner spear
#

A vector3 that is used for position is probably defined in unity units.

pure trout
#

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

inner spear
#

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.

pure trout
inner spear
#

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?

pure trout
pure trout
#

now i understand vectors

#

translations

#

and transform functions

inner spear
#

So, going back to the topic: what was transform.Translate doing again?

pure trout
#

and was also using .net things

#

in unity only

inner spear
#

I'm waiting for an answer.

#

what was transform.Translate doing again?

pure trout
pure trout
inner spear
#

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...😅

pure trout
#

right?

inner spear
#

I'm asking about what it does in general.

pure trout
inner spear
#

If you forgot, go back to the docs page and read the description again.

pure trout
#

Moves the transform in the direction and distance of translation.

inner spear
#

Okay so what it does in short?

pure trout
#

i mean it will transform

#

so i should just give it direction as well right?

inner spear
#

No... Ok. let's go back to English lessons again. What's the verb in that sentence?

pure trout
#

it is the verb

#

it does the work

inner spear
#

how is transform a verb? xD

pure trout
#

lol

inner spear
#

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?

pure trout
#

never seen it

#

its the transform

#

the transform is a input

inner spear
#

I bet you've seen it many times, since every game object has it. 😅

inner spear
#

Anyways, does that make it clear, that transform is not a verb in that sentence?

inner spear
#

Then what's the verb?

pure trout
#

transform is the inspector function

inner spear
#

no

#

it's a component

pure trout
#

verb is the translation

inner spear
#

translation is not a verb

pure trout
#

moves?

inner spear
#

It's a noun

inner spear
pure trout
#

lol

#

soo hard??

inner spear
#

Now let's go back: what does transform.Translate do in simple terms?

pure trout
inner spear
#

That's not what I asked

pure trout
#

whenever i assign it a new vector it will have 3 values

#

which we can add in transform

inner spear
#

That's not what I asked

pure trout
#

then?

inner spear
#

what does transform.Translate do in simple terms?

#

go back to the docs if you've forgotten 😄

pure trout
pure trout
inner spear
#

Okay, close. What I was expecting is something like It moves the object.

pure trout
#

so i am wrong

#

I think

inner spear
#

You're simply adding too much meaning to it.

pure trout
inner spear
#

In the simplest form what it does is move the object.

inner spear
#

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.

pure trout
#

yea

#

simplest form

#

lol

inner spear
#

So now that you know what Translate does, let's look at your code again.

pure trout
#

cam.transform.Translate(new Vector3(0, 0, dis));

#

here

#

it is moving the object with the values i am giving

inner spear
#

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.

pure trout
inner spear
#

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.

inner spear
#

every frame

pure trout
#

when the mouse button is pressed it moves the values in every frame

pure trout
#

so i need (0,0,0)

#

nope

#

not gonna work

#

it kept all the position 0

#

ohhhh now i get it

inner spear
#

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.

pure trout
#

it keeps updating the z axis value as distance all the time

#

right?

#

if i keep all the values

inner spear
#

not updating, but doing something like transform.localPosition += 0, 0, distance.

pure trout
#

all x y and z as dis

pure trout
#

it is transforming the z axis as distance only

#

but shouldnt it go fine without errors

#

?

inner spear
#

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.

inner spear
#

look at this line here:

#

You probably tested it before writing the rest of the code, right?

inner spear
#

And it worked

pure trout
#

it worked fine

#

and zoomed in and out

#

so i need space.self

inner spear
#

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?

pure trout
#

then?

#

hmmmmmmmmmm

inner spear
pure trout
#

I am using a new vector 3

inner spear
inner spear
pure trout
#

yea

inner spear
#

and also one that maps exactly to the distance between the camera and the object.

pure trout
#

i am using z value

inner spear
#

The value that you're using is bad.

pure trout
#

the same as i zoomed in and out

pure trout
inner spear
#

In one you use scroll * 6, in the other dis.

pure trout
#

ohhhhhhhh

inner spear
#

scroll * 6 is always in the range from -6 to 6

pure trout
#

i just need to use scroll * 6 value

pure trout
#

-6 to 6 only

inner spear
#

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?

pure trout
#

between the camera

#

whereas the scroll is the value we get from scroll

inner spear
#

Yes. So you're basically zooming in by distance. Is that what you want?

pure trout
#

and multiply it to get the speed

pure trout
#

i dont

#

but how to get the correct value then?

inner spear
#

First decide what is the correct value?

pure trout
#

i scrolled

inner spear
#

how much do you want your camera to move to the target each frame.

pure trout
#

maybe

inner spear
#

Then, you know what to put in there.

pure trout
#

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

inner spear
#

Share your current code

pure trout
#
    {
        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);
        }
    }```
inner spear
#

ok

#

comment out all of this bullshit:

#

Does the zoom in/out work now?

pure trout
#

wait lemme

#

zoom is working fine

inner spear
#

Okay. Now looking at your rotation code, it's complete bullshit. At least in your use case.

pure trout
#

can i continue it after 30 minutes

#

i was going to have breakfast

#

pls?

inner spear
#

Because it just rotates your camera in place. Not around the object.

inner spear
#

Because Transform.Rotate rotates the object around itself.

pure trout
inner spear
#

You should be using Transform.RotateAround if you want it to rotate around something else.

pure trout
#

now i understood the whole situation buddy

#

thanks veryyy much

inner spear
#

I guess cam.transform.Translate in the end was an attempt of a walkaround to that, but it's bad

pure trout
#

thanks to you i understood many things today

inner spear
#

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.