简体   繁体   English

Unity 2D 中的投射物

[英]Projectile in Unity 2D

I'm attempting to learn Unity (so please forgive my newbie-ness).我正在尝试学习 Unity(所以请原谅我的新手)。 I've set up my project as 2d, got a sprite moving about and I'm trying to get a projectile firing (I appreciate there are MANY SO q's about such, but I just can't get it to work, after trying many solutions).我已经将我的项目设置为 2d,有一个精灵在移动,我正在尝试发射弹丸(我很感激有很多关于这样的问题,但在尝试了很多之后我就是无法让它工作解决方案)。 I'm a complete nub when it comes to physics!说到物理,我是个彻头彻尾的人!

Here's my very simple script:这是我的非常简单的脚本:

using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {
    public Transform mObject;
    public Transform mProjectile;
    public Vector2 mProjectileSpeed = new Vector2 (10f, 10f);
    public Vector2 mSpeed = new Vector2(15, 15);
    private Vector2 mMovement;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        float inputX = Input.GetAxis("X");
        float inputY = Input.GetAxis("Y");

        mMovement = new Vector2 (mSpeed.x * inputX, mSpeed.y * inputY);

        if (Input.GetButton ("Fire1"))
            Shoot ();
    }

    void Shoot(){
        GameObject clone = (GameObject)Instantiate (mProjectile, rigidbody2D.transform.position, Quaternion.identity);
        clone.rigidbody2D.velocity = (clone.transform.forward * 1000);
    }

    void FixedUpdate(){
        rigidbody2D.velocity = mMovement;
    }
}

And this is what it's doing:这就是它正在做的事情:

古怪

No force is being added to the instantiated object and it shoots out both sides of my sprite, which I just don't understand at all.没有力被添加到实例化的对象,它射出我的精灵的两侧,我根本不明白。

I did find a solution on the Unity answers site that said to IgnoreCollider just in case the two box colliders were conflicting results, but it didn't make a difference.我确实在 Unity 答案网站上找到了一个解决方案,该解决方案对IgnoreCollider说,以防万一两个盒子对撞机的结果相互冲突,但这并没有什么区别。

I'm sure I'm doing something completely stupid, but how can I do this?我确定我在做一些完全愚蠢的事情,但是我该怎么做呢?

Many thanks!非常感谢!

Try using Addforce() method, something like this :尝试使用 Addforce() 方法,如下所示:

gameObj.rigidbody2D.AddForce(Vector3.up * 10 * Time.deltaTime); 

or或者

gameObj.rigidbody2D.AddForce(transform.forward * 100); 

or或者

gameObj.rigidbody2D.AddForce(Vector3.up * 1000);

See which combination and what values matches your requirement and use accordingly.查看哪种组合和哪些值符合您的要求并相应地使用。 Hope it helps希望能帮助到你

As @maZZZu said, Instantiate your projectile sprites ahead of your character so that your character and projectiles may not collide.正如@maZZZu 所说,在你的角色之前实例化你的射弹精灵,这样你的角色和射弹就不会发生碰撞。

Secondly, clone.rigidbody2D.velocity = (clone.transform.forward * 1000);其次, clone.rigidbody2D.velocity = (clone.transform.forward * 1000); part of your code will only allow the projectile to move in forward direction (x-axis in case to 2D and z-axis in 3D).您的代码的一部分将只允许弹丸向前移动(x 轴在 2D 的情况下和 z 轴在 3D 的情况下)。 Try using mMovement instead (if you want it to move in other directions as well).尝试使用 mMovement 代替(如果您希望它也向其他方向移动)。 eg clone.rigidbody2D.velocity = (mMovement * 1000);例如clone.rigidbody2D.velocity = (mMovement * 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM