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.
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:
Did you try Youtube?
Unity - Simple Move a Cube with the Arrow Keys - YouTube