#Hey are you around maybe i m still

1 messages · Page 1 of 1 (latest)

torpid dragon
#

Yeah of course mate. Where are you having troubles?

normal ridge
#

well i m sure if i am doing it right

#

but they way i did it doesnt create the wanted behavior

torpid dragon
#

Alright, take me through the process you're going through

normal ridge
#

Ok i have the sphere and i have an arrow parented inside of it

#

i turn it off on Awake

#

and activate it when i hiold the mouse button

torpid dragon
#

Ok ok

normal ridge
#

for now i have a script that just rotates the arrow around the player

#

Just to create the rest of the game

torpid dragon
#

Gotcha

normal ridge
#

Now i want to make it that, i click on the screen and if i hold the arrow appears pointing to the direction i m clicking at

#

so i aim the ball that way

#

and launch it when i releash the button

#

My problem is how to make the arrow turn to the mouse cursor's world location

#

while it stays at a fixed distance around the palyer

torpid dragon
#

Right, I see what you're getting at

normal ridge
#

I get the mouse world pos here

#

and then i burnout in my thoughts lol

torpid dragon
#

Well first of all, remember that since you are making the arrow a child of your player sphere, the arrow will always move with the player

normal ridge
#

Yes this is kinda intended behavior

#

The player is supposed to stick on walls after launching

#

and repeat the process

torpid dragon
#

Yeah I got that part

#

But my point is, since the arrow is moved with the player, we are more looking for the offset from the player to keep the arrow at

#

the local position

normal ridge
#

yes i understand

torpid dragon
#

Right. So, is the camera from a top down view?

normal ridge
#

Yes 90 degrees orthographic

#

and the arrow transform is this

torpid dragon
#

Alright. We can pretty much think in 2d for this scenario.

Due to it being 3d, the 2d vector representing the player's position would be (x, z)

#

While the cursor still has an x and y position

#

And in order to calculate direction, it is simply v1 - v2

#

So to put this all together in a practical way for you

#

First let us convert the player to it's "2d" positional vector, which would look something like

Vector2 playerPos = new Vector2(transform.position.x, transform.position.z)
normal ridge
#

I m following i just dont want to interupt

torpid dragon
#

Right, so now that we have this, we need to get the mouse cursor's world position.

Vector3 originalMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos = new Vector2(originalMousePos.x, originalMousePos.z);
#

Then to get the direction, we can finally do

Vector2 dir = (mousePos - playerPos).normalized;
normal ridge
#

I think i am at this state currently

torpid dragon
#

Now we want to multiply this direction by a distance to get the offset

#

Because remember

#

we only need the offset from the player

normal ridge
#

so i should multiply by the offset

torpid dragon
#

So

Vector2 offset = dir * distanceFromPlayer;
#

Then you can use the offset as local position for the arrow

normal ridge
torpid dragon
#

However, you need to remember that we want Z instead of Y

#

Ah, that might be the problem

#

Localposition sets the position relative to parent

#

while position is relative to the game world

normal ridge
#

after that how would i set the rotation to the arrow?

#

I m trying LookAt(), RotateTowards()

#

not sure what to use there

torpid dragon
#

That should be easy! You already have the direction you want it facing in.

#

So you can set the forward vector of the arrow transform to that

#
transform.forward = dir
normal ridge
#

hmm i didnt know i can do that honestly

torpid dragon
#

ah but I am seeing now you have stretched the arrow along the X axis

#

so in that case you want to align the transform.right instead

normal ridge
#

The arrow will be changed to a new graphic after i code this

#

but i ll keep that in mind

torpid dragon
#

Alright, let me know if it workd

#

works**

normal ridge
#

This is what i have for now

#

in the Update

#

This kinda makes the arrow fly up in the Y direction when i hold the mouse button ...

#

Also i dont understand here why we r working on Vector2 as it seems to make things more complicated

#

Since my y is always be fixed

#

The script is on the player btw @torpid dragon

torpid dragon
#

hmm for testing purposes, put dir in Debug.Log() and try moving around player. What is the outcome?

normal ridge
#

nothing if i move the player

#

oh wait sorry

torpid dragon
#

No I mean if you try moving the cursor around the player

normal ridge
#

dir seems to be ok

#

Look at console and where cursor is

torpid dragon
#

Yeah that seems correct to me. Try changing the

arrow.transform.localPosition = new Vector3(offset.x, 0.0f, offset.z);
#

As remember this is the offset from player

normal ridge
#

I shouldnt have made that mistake

#

this indeed is really close to the intended behavior

#

i just have rotation switching in my arrow

#

but this might be because i have rotated it in the Transform component by default?

#

I might try to remake the arrow cause the way it is now is really weird

#

like take a sphere and scale it to the z direction would probably be better

#

but the arrow works as it follows my cursor now

torpid dragon
#

Ok so that's most of the work done.

normal ridge
torpid dragon
#

So quick question

#

When you are setting the direction

#

Are you remembering to convert the Y from the Vector2 into Z?

#
arrow.transform.right = new Vector3(dir.x, 0.0f, dir.y);
normal ridge
#

yes yes

#

its probably because of the way the arrow is made

torpid dragon
#

Not in the rotation at the bottom

#

no no, look at the bottom line

#

and compare to what I sent

normal ridge
#

ok i didnt do that 😛

torpid dragon
#

sorry, had to change

normal ridge
#

i kinda understand the problem

torpid dragon
#

don't change the transform of arrow

#

simply change the last line to

#

arrow.transform.right = new Vector3(dir.x, 0.0f, dir.y);

#

and everything should work as intended!

normal ridge
#

How long have you been doing programming may i ask?

#

You fixed it mannn

#

Thank you soooo much

#

really

torpid dragon
#

Well i've been programming since I was 12, which is 10 years ago, but I started working with unity professionally at a company 2 years ago!

#

So as a hobbyist, 8 years. Professionally, 2 years

normal ridge
#

I see .. I just got a job in a company that does PPP with a known publisher

#

i m like into programming for a couple of years

#

there are things i do really well

#

but there are things where i strugle

#

and is ussually when i get into math etc

#

So you know i m feeling a bit weird about my skill atm

#

but i manage to do what they ask for now .. but i had to drag you in for example

torpid dragon
#

Don't worry about that last part, everyone feels weird about it. It's even got a term, "impostor syndrome"

normal ridge
#

ye si know

#

i ll study the script to understand it better

torpid dragon
#

Alright sounds good, you'll get the grasp of it eventually!
If you want some help or got more questions in the future, feel free to add me here on discord. Always happy to help 🙂

normal ridge
#

Thnx a lot

#

Can i actually ask one more thing about what we made here?

torpid dragon
#

Yeah of course

normal ridge
#

For example if i want to clamp the arrow which var do i look at?

torpid dragon
#

It's only 1 am now here in Japan anyways... early in the night 😛

#

What do you mean by clamp the arrow? lots of values you can clamp

normal ridge
#

Love Japan never been there .. Greece here

#

So that i cannot drag it behind the ball only in front

#

maybe i clamp the mouse pos?

torpid dragon
#

Ahhh I see what you mean.

Well, if we go back to the idea of the offset

normal ridge
#

ok yeah i get it

torpid dragon
#

hold on a second

#

You get the idea

#

if we want to clamp it in front

#

We have to limit the Y of the direction

#

So since we don't want the arrow behind the player, we want to keep the Y in the range between 1 and 0

#

And there is already a method for clamping values, which is Mathf.Clamp(valueToClamp, minValue, maxValue)

normal ridge
#

Yes i know the method ... I guess i can do that

torpid dragon
#

So going back to where you calculate the direction. After getting the initial value, you can clamp the Y

Vector2 dir = (mousePos - playerPos).normalized;
dir.y = Mathf.Clamp(dir.y, 0, 1);
normal ridge
#

But it might not be the best solution since the ball will stick to walls and then i need to change how values are clamped

torpid dragon
#

However, this will leave the magnitude of the directional vector less than 0

#

which will mess with your distance

#

So to fix that, you need to normalize the vector once more

#
Vector2 dir = (mousePos - playerPos).normalized;
dir.y = Mathf.Clamp(dir.y, 0, 1);
dir.Normalize()
normal ridge
#

Ok good i screenshoted that

#

and i ll think a bit after clearing my mind to see how i proceed with this

torpid dragon
#

Sure thing. Just to give you a pointer for how I would solve that for you to look into is the getting the normal value of the wall you are stuck to.

normal ridge
#

I sent you an friend invite but i promise i wont bother you for stupid stuff

#

Since the ball is sticking to walls, clamping might not be the way to go

#

I think i ll use raycasting to be sure that the player is not clicking on walls and only in the ground

#

and probably allow him to shoot any direction in the level he wants

torpid dragon
#

That's the fun part if you ask me, problem solving.

normal ridge
#

Indeed

#

You know i m 31 years old

#

I switched to game development 3 years ago from zero

#

it was my passion

#

so i m doing it all day long now

torpid dragon
#

But I accepted your friend request! Don't worry about any stupid questions, i'm happy to help with anything

normal ridge
#

i hope i reach your level some day

torpid dragon
#

That's great though, what did you do before?

normal ridge
#

Photographer

#

for like 10 years

#

So i know about cameras lights etc

#

and been a gamer since NES

#

so i came into this to be a Designer

#

but i had to learn everything in the end

#

to make something solo and get a job

#

Its going well but i soimetimes run into those stupid things i struggle to solve

#

Anyways i wont keep you here you need to sleep

#

Arigatōgozaimashita ❤️

torpid dragon
#

i'm sure you'll get there with practice. But again, feel free to reach out. I'm available at most times ^^

#

ありがとうございます ^^

normal ridge
#

Hey thnx a lot

#

Your writen languange looks like art lol