- Home /
Move object with velocy towards predefined destination,Gameobject move to a preset position but retaining velocity for physics
Hi everyone. Making a 2D game and I currently have a script that spawns prefabs on mouseclick, as per the provided code.
//Gameobject for storage of the object to be displayed
public GameObject clickNumber;
//Temporary gameobject for storage of the instantiated object
private GameObject number;
//Stores the mouse position upon clicking
private Vector3 mousePos;
//Stores the mouse position relevant to the camera
private Vector3 objectPos;
//Sets the life of the spawned object in seconds
public int coinLife = 5;
void Update()
{
//Determines if left click has been pressed
if (Input.GetButtonDown("Fire1"))
{
//Stores the position of the mouse
mousePos = Input.mousePosition;
//Defines the layer spawn distance
mousePos.z = 1;
//Spawns objects only if in the clickable area
if (mousePos.y > 250)
{
//Defines the spawn position relevant to the camera
objectPos = Camera.main.ScreenToWorldPoint(mousePos);
//Insntiates the object according to the stored position
number = (GameObject)Instantiate(clickNumber, objectPos, Quaternion.identity);
//Destroys each spawned object after its predefined lifespan
Destroy(number, coinLife);
}
}
}
What I am hoping to achieve is some way for the spawned objects to always move to a predefined point on the screen, so that no matter where is clicked, the object spawns and moves in a line or on an arc and ends up in the same place. Whilst I am aware that there are a couple of ways to predefine object start and end points, I also would like the object to retain some velocity, so that it collides and then bounces upon impact with the point.
TLDR: I want to simulate an 'object throwing' function with a preset destination, but variable starting point.
Thank you in advance for any advice you might have
Your answer
Follow this Question
Related Questions
Move 2D ground up and down 1 Answer
I do you control movements and apply other forces at the same time ? 0 Answers
Need help with my movement script.. :( 1 Answer
Move Player without acceleration 1 Answer
Character Jump Control 1 Answer