Bullet not moving unity

2019-08-28 22:51发布

At the moment I have a script that is firing a sphere, when i press the button which makes it fire; the pellet comes out but does not move across the screen, it just stays in a stationary position. whereas I would like it to move across the screen forward...

Script:

    #pragma strict

public var pellet : Transform;


function Start () {



}

function Update () {    

    if (Input.GetKeyUp("o"))
    {
        var pelletfire = Instantiate (pellet, gameObject.Find("pellet_Spawn").transform.position, Quaternion.identity); 
        pelletfire.rigidbody.AddForce(transform.forward * 500); 
    }


}

Thanks for any suggesitons

1条回答
一纸荒年 Trace。
2楼-- · 2019-08-28 23:35

Replace your line with this-

pelletfire.rigidbody.AddForce(transform.forward * 100,ForceMode.Impulse);

For more details look at forcemode documentation in unity-

http://unity3d.com/learn/tutorials/modules/beginner/physics/addforce http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.html

you also require a GameObject in scene with name "pellet_Spawn". and your prefab should have a rigidbody attached to it. and the script needs to be attached to a GameObject in the scene. and set the prefab from the inspector.

查看更多
登录 后发表回答