- Home /
Make a rolling ball always on ground without falling when reaching the edges of the map
Greetings everyone and a happy mother's day to the mothers in this community d(^^)
So i'm working on a 3D project that involves a ball rolling around a cube. The player presses a key and it activates a kinda of magnet to the ground, that means the player gets stick to the ground and continue to follow the ground without falling to infinity and beyond because of the gravity, the ball has a rigidbody and the cube only has a box collider.
When the player presses ,i.e T, there'll be a bool that'll store if the player has presses the key and if so, then the player is in stick mode, let's call it that for kicks and giggles.
Now my question is, how to I achieve this effect? I've been researching and I saw people using Raycast as a solution, but I'm not able to achieve what I need.
Here's my move player code
private void Update()
{
MovePlayer();
}
void MovePlayer()
{
float moveHorizontal;
float moveVertical;
if (!invertedCamera)
{
moveHorizontal = Input.GetAxis("Horizontal") * invertX;
moveVertical = Input.GetAxis("Vertical") * invertZ;
}
else
{
moveHorizontal = Input.GetAxis("Vertical") * invertZ;
moveVertical = Input.GetAxis("Horizontal") * invertX;
}
Vector3 movePlayer = new Vector3(moveHorizontal, 0.0f, moveVertical);
playerRB.AddForce(movePlayer * movementSpeed);
check_if_stick(); //I don't Know How To program this part =(
change_camera_view();
}
void check_if_stick()
{
if(Input.GetKeyDown(KeyCode.T) && !keyDown)
{
if (!isStick)
{
isStick = true;
//What should i do here?
}
keyDown = true;
}
if (Input.GetKeyUp(KeyCode.T) && keyDown)
{
keyDown = false;
}
}
Now my problem is, how to create the RayCast, also is this the best way to achieve what I want? Or is there another way? Should i create this check_if_stick on a new c# script file?
Thank you so much for your time and help, i'm still a little noobish but getting there =) Best reguards =)
Answer by anthot4 · May 08, 2018 at 11:44 AM
Is the ground flat the whole time and not moving? There are a few ways you could do this.
greetings anthot4, thank you for your reply it's like a cobble so a cube with, let's say, 20 width , 20 length and 1 height
I think the easiest way to do with would be to add a downwards rigid body force. I'm not sure how you would do this by raycasting. It would be best to put the CheckIfStick() function in your current script.
downwards rigid body force?
Would you $$anonymous$$d explaining please? =)