Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

AddForce to instantiated GameObject

Discussion in 'Scripting' started by Foxi, Sep 13, 2020.

  1. Foxi

    Foxi

    Joined:
    Feb 13, 2014
    Posts:
    3
    Hi, i want my character to throw shurikens from his hand in the direction of mouse.
    Now it just spawns and fall down without any force.

    Code (CSharp):
    1. public GameObject starPrefab;
    2.     public Transform hand;
    3.     public Transform player;
    4.     public float speed;
    5.  
    6.  
    7.  
    8.  
    9.     void Update()
    10.     {
    11.  
    12.  
    13.  
    14.         Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 12);
    15.         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    16.  
    17.         if (Input.GetMouseButtonDown(1))
    18.         {
    19.             Instantiate(starPrefab, hand.transform.position, player.rotation);
    20.             starPrefab.GetComponent<Rigidbody>().AddForce((mousePos) * speed);
    21.         }
    22.  
    23.  
    24.  
    25.     }
    if I add ,,starPrefab =" before Instantiate, It's just spawning (clone), (clone)(clone),(clone)(clone)(clone) etc. and correctly adding force to the mouse. I wouldn't have a problem with that if spawning stopped working when last (clone) is destroyed (simple timed self destroy script on the star prefab). So how do I add the force to this instantiated object?
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    513
    Prefabs are the blueprint and
    Instantiate
    returns the instance created from the blueprint. Right now, you're trying to add force to the prefab, which will have no effect. If you overwrite the
    starPrefab
    variable, you will then create copies of the instance in the scene, and when that instance is deleted, you won't be able to create more.

    Instead, assign the instance to a new variable:
    Code (CSharp):
    1. var instance = Instantiate(starPrefab, hand.transform.position, player.rotation);
    2. instance.GetComponent<Rigidbody>().AddForce((mousePos) * speed);
     
    Foxi likes this.
  3. Foxi

    Foxi

    Joined:
    Feb 13, 2014
    Posts:
    3
    That works, kind of, finally spawning and throwing not just falling. No matter if character facing left or right, on right screen the force is as it should be, on left side the force is weaker.
    Failed to upload video, so at least this: https://media0.giphy.com/media/H682ThA0xKhgBtZzcn/giphy.gif Not the best quality, but still can see what the problem is.

    Edit: Problem solved as soon as I posted this reply. In the code
    Code (CSharp):
    1. var instance = Instantiate(starPrefab, hand.transform.position, player.rotation);
    I just added transform to the player.rotation
    Code (CSharp):
    1. var instance = Instantiate(starPrefab, hand.transform.position, player.transform.rotation);
     
    Last edited: Sep 13, 2020
unityunity