#Hey are you around maybe i m still
1 messages · Page 1 of 1 (latest)
well i m sure if i am doing it right
but they way i did it doesnt create the wanted behavior
Alright, take me through the process you're going through
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
Ok ok
for now i have a script that just rotates the arrow around the player
Just to create the rest of the game
Gotcha
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
Right, I see what you're getting at
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
Yes this is kinda intended behavior
The player is supposed to stick on walls after launching
and repeat the process
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
yes i understand
Right. So, is the camera from a top down view?
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)
I m following i just dont want to interupt
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;
I think i am at this state currently
Now we want to multiply this direction by a distance to get the offset
Because remember
we only need the offset from the player
so i should multiply by the offset
So
Vector2 offset = dir * distanceFromPlayer;
Then you can use the offset as local position for the arrow
Ok i didnt do that before i used the position
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
after that how would i set the rotation to the arrow?
I m trying LookAt(), RotateTowards()
not sure what to use there
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
hmm i didnt know i can do that honestly
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
ok i ll try that
The arrow will be changed to a new graphic after i code this
but i ll keep that in mind
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
hmm for testing purposes, put dir in Debug.Log() and try moving around player. What is the outcome?
No I mean if you try moving the cursor around the player
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
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
Ok so that's most of the work done.
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);
Not in the rotation at the bottom
no no, look at the bottom line
and compare to what I sent
ok i didnt do that 😛
sorry, had to change
i kinda understand the problem
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!
How long have you been doing programming may i ask?
You fixed it mannn
Thank you soooo much
really
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
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
Don't worry about that last part, everyone feels weird about it. It's even got a term, "impostor syndrome"
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 🙂
Yeah of course
For example if i want to clamp the arrow which var do i look at?
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
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?
Ahhh I see what you mean.
Well, if we go back to the idea of the offset
ok yeah i get it
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)
Yes i know the method ... I guess i can do that
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);
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
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()
Ok good i screenshoted that
and i ll think a bit after clearing my mind to see how i proceed with this
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.
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
That's the fun part if you ask me, problem solving.
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
But I accepted your friend request! Don't worry about any stupid questions, i'm happy to help with anything
i hope i reach your level some day
That's great though, what did you do before?
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 ❤️