#Edge of Radius
1 messages · Page 1 of 1 (latest)
I have this script where the player can shoot a ball, if the player clicks outside max shoot radius the point should just be at the edge. I am not sure how to make it be at the edge of that radius
https://cdn.discordapp.com/attachments/497874004401586176/1025820540918825040/unknown.png
This shoot radius, is it a circle? @fickle pike
Well currently if you shoot inside the radius it will just place a debug ball in that location, but if you click outside the radius I want to find the closest point to the edge of the radius and place the ball there'
Is it a circle?
Post full scripts in a pastebin link
https://gdl.space
Since you are speaking of Radius one might assume it is a circle, but since I have no idea, and for the sake of not working with false assumptions, is it?
I would like it to be a circle yes. This is the whole script for now, some stuff is not used and other is just for debugging
Are you 100% here now?
Im here yeah?
Great. I hit many dead ends lately, and I dont' have time to wait for people. Anyway
Ah makes sense
Does this circle exist yet in any way?
Well it only exists when checking if the hit.point is outside the maxAtkDist float
Else there is no gameobject or anything else
is this 2D or 3D?
3D
kk, trying to visualize
Anyway, my initial thought is the following
I'm just not sure it will work in 3D
I can like paint a small pic of what im trying to do to further explain
A screenshot of the scene could help
When the mouse button is clicked,
the raycast is performed,
and if something is hit,
then the object should be spawned at position:
Well
(I'm typing code)
ah ok
float distance = Vector3.Distance(transform.position, hit.point);
float spawnDistance = distance;
if (distance > maxAtkDist)
{
float overshoot = (distance - maxAtkDist)
spawnDistance -= overshoot;
}
executeAttack()
need to google some Vector logic
code is unfinished, but you get the gist of it
Notice how I split up the formula inside the if-statment. That's for being able to read the logic, without understanding the full formula
That's what I was about to google how to calculate
really hazy this Saturday xD
If that solves it, good stuff. Let me know once that's done, or if you need help with the last bit.
Sure, just need to clean some cloths real quick
changed the Vector3s to floats, because d'uh
Ight im back and going to try it now
Changin them to floats won't work though, I only care about the X and Z axis and the executeAttack function requires a vec3 to change the position of the debugOBJ
This is what I got with trying another way
This is the new script ```cs
void onPlayerAttack(InputAction.CallbackContext ctx)
{
Ray ray = cam.ScreenPointToRay(pManager.mousePos());
if (Physics.Raycast(ray, out hit))
{
float distance = Vector3.Distance(transform.position, hit.point);
if (distance > maxAtkDist)
{
// Get point on hit.point direction closest to sphere radius (maxAtkDist)
float overShoot = (distance - maxAtkDist);
Vector3 newPos = new Vector3(hit.point.x - overShoot, hit.point.y, hit.point.z - overShoot);
executeAttack(newPos);
}
else
executeAttack(hit.point);
}
}
void executeAttack(Vector3 hitPoint)
{
debugOBJ = GameObject.CreatePrimitive(PrimitiveType.Sphere);
debugOBJ.transform.position = hitPoint;
}```
For some reason that makes it spawn in 3 random positions
This is what I have been kinda thinking but I hoped there was a function in the Mathf library that could just get me the closest point to the radius
@plucky dust
what should newPos be then?
Checking that atm
Vector3 direction = destination - origin;
I knew how to get the direction, i just don't fully know how to get the right position
it's a tough google tbh
Yeah thats why i came here lol, I didn't know what to google
Vector3 origin = transform.position;
Vector3 direction = new Vector3(1,1,0);
float distance = 0.5f;
Vector3 destination = origin + (direction * distance);
you need to modify direction
Vector3 direction = destination - origin;
A good use of time would be to find a basic Vector math tutorial that probably contains the answer.
So this?
Vector3 Direction = transform.position - hit.point;
Vector3 destination = transform.position + (Direction * maxAtkDist);```
Ight
destination - origin
oh ok
Yeah, I guess i have mixed them this whole time them lol. Ill go for google and let you free now lol
I believe this is wrong, sec.
float distance = Vector3.Distance(transform.position, hit.point);
float spawnDistance = distance;
if (distance > maxAtkDist)
{
float overshoot = (distance - maxAtkDist)
spawnDistance -= overshoot;
}
executeAttack()
float distance = Vector3.Distance(transform.position, hit.point);
float spawnDistance = distance;
if (distance > maxAtkDist)
{
float overshoot = (distance - maxAtkDist)
spawnDistance -= overshoot;
}
Vector3 direction = (hit.point - transform.position).normalized;
Vector3 destination = transform.position + (direction * spawnDistance)
executeAttack(destination)
I think that's final, if you can implement that in your code.
Alright Thanks so much :D
You're welcome. Good luck, and have a nice weekend 👋
Yeah you too :D
You weren't wrong about the Trigonometry part
Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.
Is answers.unity.com a dead site?
No, I was just on it. Probably just network issues somewhere.
I haven't actually run this code, but here is a polished version
private Vector3 NewPos(Vector3 origin, Vector3 destination, float maxDistance = 0f)
{
float distance = Vector3.Distance(destination, origin);
float clampedDistance = distance;
bool maxDistanceIsRelevant = maxDistance > 0f;
bool distanceIsHigherThanMax = distance > maxDistance;
if (maxDistanceIsRelevant && distanceIsHigherThanMax)
{
float overflow = (distance - maxDistance);
clampedDistance -= overflow;
}
Vector3 direction = (destination - origin).normalized;
Vector3 clampedDestination = origin + (direction * clampedDistance);
return clampedDestination;
}
@fickle pike There was one error in the solution I gave you.
// Wrong.
Vector3 direction = destination - origin;
// Correct.
Vector3 direction = (destination - origin).normalized;
Corrected the last two script segments here.
Ah ok thanks, and with the answers.unity.com I haven't been able to get on the last two weeks. I have been on many different networks thees two weeks so its kinda weird if they all had problems with that one site
That's odd. Are you still not able to load the site?
nope
may I ask which country or continent you're on?
actually, nevermind
it will only lead to speculation
Try VPN
Epic Privacy Browser has 1-click VPN built in.
It should be fine in denmark
Hm. Using custom DNS or anything?
Nope all stock to keep it easy for my parents
if you're on Windows you can open cmd and type tracert learn.unity.com to see where it stops in the chain.
im on linux lol
not on an arch based distro
I haven't used this one the past 6 weeks doe to me living on a school, its a concept really hard to explain because its not found outside denmark
But it also doesn't load on my laptop
or phonr
oh wait now its up on my phone
Folkhighschool?
If you've been on 4 networks with the same Arch Linux installation, perhaps there is something missing in Linux.
if that was the case
Efterskole is the danish name for it but from what I know it doesn't translate that well
kk
it doesn't even leave your computer?
That's odd. And this is spiraling into very off-topic.
But for the sake of testing your arch build for flaws, keep a Live USB of some distro that just works with everything, so that you can quickly see if the Live version can achieve things your Arch installation cannot.
Im sure I can fix it at some point
anyways the code you sent was a little cleaner than what I made, but both works
Thanks again :D
Good stuff. Have a nice evening 👍
you too
PS:
private Vector3 NewPos(Vector3 origin, Vector3 destination, float maxDistance = 0f)
// = 0f
// Doesn't make sense in this context, as you'd never want to ignore that parameter. Remove.
private Vector3 NewPos(Vector3 origin, Vector3 destination, float maxDistance)
Yeah I figured lol