Location Movement on creating object using script

Hi,

I can drag and drop the basketball now.

My second attempt on Unity is: creating a basketball and moving it to different location. However, I encounter some problems on doing it.

In the beginning, I can create a basketball by pressing “g” using the script. However, it creates a basketball and move my orginal basketball to different location. And I pressing “g” one more times, it creates two basketball and move the orginal two baskballs to different location.

Can I create ONE basketball with the specific location such as my cursor location? How to update my script to implement it?


Make a boolean. It is true at the beginning. As long as it is true , you are allowed to create a ball.

When you create a ball, you set it to false, therefore, you can’t make a ball anymore.

About the screen position, have a look at : Unity - Scripting API: Camera.ScreenToWorldPoint

Hi,

I can create one basketball when I press “g” now.

However, when I try to get the mouse position, I encounter the error message.

MissingComponentException: There is no ‘Camera’ attached to the “Sphere” game object, but a script is trying to access it.
You probably need to add a Camera to the game object “Sphere”. Or your script needs to check if the component is attached before using it.

BallCreation.Update () (at Assets\BallCreation.js:6)

I think I have to put the basketball object be a child of the Main Camera. But the error still happens when I do it. Is there any methods for me to create the basketball on the specific location (e.g. cursor location)?

Either you put this script in the camera, and you access the camera component by writing “camera”, or you use Camera.main :wink:

Edit : oh, and, in your case, you’d better replace camera by Camera.main

Hi,

May I know the meaning and the effect of the spring, damper and distance in the script “Drag Rigidbody”?

Now, I only can drag the object that is very close to me. I would like to drag the object that is far away to me also. Is it possible for me to do it if I set the distance to a large value?

Thanks.

A spring applies a force along its length which is proportional to the distance between the two endpoints. The base force that gets multiplied by the distance is the “spring” value from DragRigidbody. However, this force needs to be reduced as the attached objects start moving, or else the spring will keep bouncing indefinitely. This is done by subtracting part of the spring force in proportion to the speed at which the endpoints are coming together. The base reduction in force is the script’s “damper” value. The “distance” value is simply the maximum distance that the spring can stretch.