Search Unity

Another Addforce does nothing thread. (again)

Discussion in 'Physics' started by tclancey, Jun 12, 2020.

  1. tclancey

    tclancey

    Joined:
    May 19, 2017
    Posts:
    143
    My last post on this was deleted, thank you so much, so I'll try again.

    I have the following code which pops a ball into existence and applies a force. Or at least it should.

    Code (CSharp):
    1.        if (MakeBall)
    2.         {
    3.             Vector3 f = transform.forward.normalized;  //  Used to move ball free from barrel.
    4.             Instantiate(ball, transform.position + (f * 2.2f), Quaternion.identity);  //  Move ball free from barrel
    5.             Rigidbody rb = ball.GetComponent<Rigidbody>();      //  Yes it does have one!
    6.             rb.AddForce(Vector3.forward * EjectSpeed, ForceMode.Impulse);  //  <<  AAARRRGGGHHH
    7.            
    8.             MakeBall = false;
    9.             EjectTime = Time.time;
    10.         }
    All is good, the ball is created, but then falls majestically to the floor without a single whiff of forward momentum.

    I have spent hours googling 'Addforce does nothing'. There are a LOT of threads. Nearly all of which say I should try using Velocity instead, all the others say I should definitely not use Velocity. There are loads of tubes which show people adding a force to an existing object. I've found nothing for a dynamic object which seems odd as that's what projectiles are going to be, and projectiles need force. And nearly every game has a projectile!

    There seem to be no fixes for any of the threads I've found, just explanations of what Addforce does. Or doesn't. Do I need to go back to the dark ages and calculate the physics myself? I chose to move to unity so I didn't have to deal with physics in code anymore.

    Could someone please explain why the above doesn't work? I can only assume I'm being a complete ass and I'm missing something very obvious as I've tried absolutely every single combination of force, velocity and shouting.

    Many thanks.
     
  2. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    you are making it hard. attach an empty game object name it muzzle and make it child of barrel and then instantiate ball and add force transform.forward
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    It falls to the ground because you're adding momentum to ball's prefab. And not to the newly created ball.

    Instantiate returns result, and that result is the newly created object.

    See:
    Code (csharp):
    1.  
    2.             Instantiate(ball, transform.position + (f * 2.2f), Quaternion.identity);  //  Move ball free from barrel
    3.             Rigidbody rb = ball.GetComponent<Rigidbody>();      //  Yes it does have one!
    4.  
    5.  
    In those lines you ignore object returned by instantiate, and isntead grab rigibody from prefab object.
     
    KyleOlsen and dahiyabunty1 like this.
  4. tclancey

    tclancey

    Joined:
    May 19, 2017
    Posts:
    143
    Ahhhh, so Instantiate doesn't give me a new object called ball.

    I need NewBall=Instantiate(....
    How is that easier? That's just repeating what I already have with other objects.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    It can't return your object via "ball" parameter.

    See, in C# in this kinda of scenario:
    Code (csharp):
    1.  
    2. void someFuntion(SomeType arg){
    3. }
    4.  
    function can access and alter "arg" object, and if "arg" is of reference type, changes will be visible outside of function:
    But. Unless parameter is marked as ref or out, it can't REPLACE "arg" with something else.

    Example:
    Code (csharp):
    1.  
    2.  
    3. void func1(SomeType arg){
    4.     arg = new SomeType();
    5. }
    6.  
    7. void func2(ref SomeType arg){
    8.     arg = new SomeType();
    9. }
    10.  
    11. void func3(out SomeType arg){
    12.     arg = new SomeType();
    13. }
    14.  
    15. void test(){
    16.     var obj1 = new SomeType();
    17.     var obj2 = new SomeType();    
    18.     var obj3 = new SomeType();
    19.     func1(obj1);//obj1 doesn't change
    20.     func1(ref obj2);//obj2 replaced with a new object
    21.     func1(out obj3);//obj2 replaced with a new object
    22. }
    23.  
     
  6. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    977
    It would be very odd if Instantiate changed your local variable "ball". It's possible to do so in functions using the ref keyword, but it wouldn't make sense for a function such as instantiate.

    Here's an example. This makes far more sense:
    Code (CSharp):
    1. var myCopy = MakeCopyOf(someObject);
    Than this:
    Code (CSharp):
    1. MakeCopyOf(ref someObject); // someObject now contains your copy.
    Instantiate is essentially a MakeCopyOf.
     
  7. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Your last thread was deleted by a moderator because it was posted in the wrong forum. Please post physics questions in the physics forum.

    I'll move this thread there for you for now.