简体   繁体   English

将游戏对象移动到 unity 2d 中的单击位置

[英]Move gameobject to click position in unity 2d

Is it possible to move a gameobject to click position in unity 2D while gameobject is a rigidbody 2d with gravity and movement looks like jump from gameobject's position to click position.是否可以将游戏对象移动到统一 2D 中的单击位置,而游戏对象是具有重力的刚体 2d,并且运动看起来像是从游戏对象的位置跳转到单击位置。 Any help reference will be really helpful :)任何帮助参考都会非常有帮助:)

something a bit like this?有点像这样?

Transform projectile;
float speed = 5f;

void Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        Vector3 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        target.z = projectile.position.z;

        Vector3 offset = target - projectile.position;
        Vector3 direction = offset.normalized;
        float power = offset.magnitude;
        projectile.GetComponent<RigidBody2D>().AddForce(direction * power * speed);
    }
}

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

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