#Mathf.Clamp

1 messages · Page 1 of 1 (latest)

candid fox
#

Let's do a thread

wind sluice
#

thats fine with me

#

heres my code:

transform.position = new Vector2(-5f, topPositionY); // This is what I have right now. it just jumps to where it needs to go with no transition.

transform.position += new Vector3(-5f, moveSpeed * Time.deltaTime, 0); // I'm trying to make it so instead of it glitching, it just goes smoothly and stops at the variable which is the y position. Where would I add the variable topPositionY in the equation here?
candid fox
#

Looking at the documentation, they did not exactly make a simple example.

wind sluice
#

yeah its using all these math terms that brain cannot comprehend at 2:45am lmao

candid fox
#

so you want the position to never exceed a certain Y position

wind sluice
#

yeah i bassically have three positions top middle bottom. the player can switch between those values. before it would glitch to each one just cause i had it set up that way. i wanna feel how it would feel to smoothly transition between those values

#

would it be easier for you if I sent the script?

candid fox
#

Not sure I understand

wind sluice
#

here ill send the script

candid fox
wind sluice
#

wait what is this console

#

can you see it on your end?

candid fox
#

no, you need to save it and give me the link

wind sluice
#

ohh its just like pastebin okay

candid fox
#

CTRL+S works for save

#

part of why I like it

wind sluice
#

can you see this?

candid fox
#

Yes

wind sluice
#

and i just have my old way commented out while i figure out this new way for clarification

#

so usually i have those things not commented out and this new line is not there

#

oh and the float y variable i just forgot to delete but isnt used anymore

candid fox
#

yeah i bassically have three positions top middle bottom. the player can switch between those values.
What happens when the player switches?

#

I'm not 100% certain I understand what you're doing. Will read over the code.

wind sluice
#

heres a video

#

sorry im really bad at explaing this stuff

#

this is what my code currently does

#

i want my circle to move between those three points smoothly if possible similar to how the red squares come at the player smoothly and dont jump forward

wind sluice
candid fox
#

anyone know how i would make the code stop at the topPositionY variable? All it is a value that holds where I want to stop. The code be

#

Do you mean, not be able to go up another notch?

#

when it is supposed to be at its highest position

wind sluice
#

yes. if its at the top position, they shouldnt be able to keep going up. if its at the top position, pressing up should do nothing as only down is available now. This works with the previous implementation, I just don't know how to make it stop with the new way that makes it move smoothly.

candid fox
#

Alright.

#

Make an integer variable

wind sluice
#

okay

candid fox
#

have it start at 2

#

if you start in the middle

#

when you press W
if the integer does not equal 3
then move up

when you press S
if the integer does not equal 1
then move down

wind sluice
#

does this make it move smoothly?

#

cause isnt that what im doing. the code is working i just need it to move between levels smoothly

candid fox
#

No, that constrains the movement so that you don't move too far up or down

#

Then you have missed the finer points of our conversation, or not clearly defined what you need

#

anyone know how i would make the code stop at the topPositionY variable? All it is a value that holds where I want to stop. The code be

#

Anyway

wind sluice
#

ohh

#

im really sorry i just wasnt clear

#

the code works it stops when it needs to. it just doesnt with it moving smoothly instead of teleporting

candid fox
#

That's alright. It's fairly common with new people who stay up late.

If you want the ball to animate towards its new position, instead of teleporting, then you need to move it frame by frame, using Time.deltaTime to divide the length of moment between the frames.

#

In your case, you'd need a very basic and slightly creative animation "system" in your code

wind sluice
#

i see what you are saying

#

so what I have is this:
transform.position += new Vector3(0, moveSpeed * Time.deltaTime, 0);

should I scrap this or can I work with this to make it do what you are saying.

candid fox
#

so where you change its position, you should instead change it's newPosition
while also flipping a bool hasNewPosition

then create a new method, which you'll execute in Update()
in this method, you simply say:
if (hasNewPosition)
and then step or Lerp the transform.position towards the newPosition

wind sluice
#

ohh so thats how ill implment lerp. cause thats what i was reading. and ive used lerp a long time ago but i just couldnt remember how to get it to work in this situation

#

thank you so much for the help man. sorry for the confusion. i am very tired LMAO

candid fox
#

You're welcome, np.
Probably best to get some sleep before you continue further.

wind sluice
#

haha yes! i started working on this mini project and i was just like ima grind this out lol. its funny how ive been doing c# and unity for a good bit now but having no sleep just messes with my brain. have a good night man!

candid fox
#

sleepiness and hunger are the worst
Good night!