- Home /
how can I Keep sphere attached to ground?
hey all so im making a crazy golf type game and came across another issue, when i apply power to the ball to hit it, the ball works as usual however usually hits a wall and flies up over the edge. What i want is the ball to stay on the ground at all times
Ive tried to lock the y axis keeping the ball down however the courses sometimes feauture upward/downward hills which for obvious reasons wont work as the ball is locked on the Y axis. Any ideas how i can solve this?
Answer by sparkzbarca · Aug 01, 2013 at 02:07 PM
this allows you to have grounds of unlimited height up or down and always touch the ground.
cast a ray from ball center to the ground.
ground point = raycast.point;
cast a ray from ground point to ball center
ball point = raycast.point;
get the distance between the 2
vector3.distance(groundPoint,Ballpoint)
if the distance is greater mathf.epsilon (a really small number) then the ball isn't touching the ground.
move the ball down. Down is
-ball.transform.up
distance is what you calculate earlier
ball.transform.position = -ball.transform.up * distance;
i see what you mean, im going to try knock out that code now. it seems perfect for what i need. thankyou. will post results =]
RaycastHit hit; if (Physics.Raycast(transform.position, -Vector3.up, out hit)){ //loat distanceToGround = hit.distance; print (hit.distance); }
if (hit.distance > 0.51){
//if (hit.distance > 0.5)
Debug.LogWarning("your on the right track luke, keep it up =]");
Ball.transform.position = -Ball.transform.up * hit.distance;
}
thas what i have so far and it works to a point, ins$$anonymous$$d of snapping down it will snap down the the right of the course
sorry that was a messy version, ive tidied up a bit
// Update is called once per frame void Update () {
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit)){
print (hit.distance);
}
if (hit.distance > 0.51){
//if (hit.distance > 0.5)
Debug.LogWarning("your on the right track luke, keep it up =]");
Ball.transform.position = -Ball.transform.up * hit.distance;
}
Answer by wijesijp · Aug 01, 2013 at 11:01 AM
How about adding a constant downward force to the ball?
tried this and i can get it to work however, the course assets have a physics material that gives bounce off to the ball (for the sides) and when i apply force downward the ball starts bouncing franticly lol
believe me ive tried lol, without it the ball just doesnt seem to act the way i want it to. i need that bounciness XD
Answer by RPGstandard · Aug 01, 2013 at 10:31 AM
put a bound thresh hold on the ball object so it cant go beyond a certain Y coordinate.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
Help with city building game? 1 Answer
Gun shooting sideways. NEED HELP!!! 2 Answers