Bullet not moving unity

六眼飞鱼酱① 提交于 2019-12-11 16:45:03

问题


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:


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.



来源:https://stackoverflow.com/questions/17403562/bullet-not-moving-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!