- Home /
How do you move a ball in a 3d inviorment using the player charater
Hi, so I have this ball in my game and I want to move it when you get close. I have a player casting script so use that for a reference if you have to. I do not want to move it when you press a key. I want to kick it like a soccer ball. So if you have any idea on how to do that, please tell me.
Did you try Youtube? https://www.youtube.com/watch?v=sXQI_0ILEW4
"Did you try Youtube" is not an answer to the question. Please mark it as a comment so the question will still appear as an unanswered question and people will receive the help they need.
No, I want to move a cube, ball, ext. with a 3d player. I want to move it when you get close like kick the ball. but it would happen when you get close, meaning there would be no key or text to accompany it. But thankyou for the help. @Jana_AI_Bdour
Answer by Lightning_A · Jun 20, 2019 at 07:12 PM
You could try detecting when the distance between the soccer ball and your player is less than a certain value with Vector3.Distance; inputting your character's position and the position of the soccer ball, and when the 2 objects are really close, you could use Rigidbody.AddForce to apply a force to the soccer ball. (Just make sure your soccer ball game object has a rigidbody component.)
Something like this:
public GameObject soccerBall;
public float distanceBeforeKick;
public float thrust;
void FixedUpdate()
{
if(Vector3.Distance(transform.position,soccerBall.GetComponent<Transform>().position) < distanceBeforeKick)
{
soccerBall.GetComponent<Rigidbody>().AddForce(transform.forward * thrust);
}
}
I'm sure there are ways you could optimize that code, but something like that should work.
Here's the documentation for AddForce and Vector3.Distance: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html https://docs.unity3d.com/ScriptReference/Vector3.Distance.html
So I gave the ball a rigidbody but it wouldn't move. So I took away the box collider but it fell threw the floor. Could you help ne please. ) :
Try messing around with the rigid body. If you have ball and add a rigid body component to it, you can add a force to that rigid body in the forward direction just like @Lightning_A is doing. Then you can play around with how much force you are going to use, and the mass of the rigid body. If the "thrust" in the script above is 1, and the mass of your rigid body is 1, it's probably not going to move. Try changing the force to something like 1000 and see what happens.
So I changed the trust to 1,000,000 and made a void update with my player casting code that looks like this:
distanceBefore$$anonymous$$ick = PlayerCasting.DistanceFromTarget;
but I have two problems.
It doesn't roll (Which I think I need to change by animation, But I don't know were to put it)
It moves threw walls and I don't know how to fix it.
Can you help me on this?
Your answer
![](https://koobas.hobune.stream/wayback/20220612203508im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Player movement on tilting platform 0 Answers
How to change character forward movement and make turning smoother 0 Answers
Projectile Firing High 0 Answers