简体   繁体   English

Unity C#:射弹速度相对于鼠标之间的距离

[英]Unity C#: Projectile speed relative to distance between mouse

I have a problem in Unity where im trying to make instantiate a projectile with a direction and a speed. 我在Unity中遇到一个问题,即我试图用方向和速度实例化弹丸。 The direction is dependant on the RotateMouse() function which calculates a direction (for the projectile) in relation to the mouse. 方向取决于RotateMouse()函数,该函数计算相对于鼠标的方向(对于射弹)。 After it is normalized, i send it to another a "projectile movement" script which just multiplies it with a speed. 标准化后,我将其发送到另一个“弹丸运动”脚本,该脚本会将其乘以速度。

For some reason when i have the mouse close to the character in a certain direction it goes really slow, and the opposite with a long distance... I have also debugged all the values, and every "direction" variable is sub 1 in x,y and z... 由于某些原因,当我在某个方向上将鼠标靠近字符时,它的运行速度非常慢,而相反方向的距离很长...我也调试了所有值,并且每个“方向”变量在x中都小于1 ,y和z ...

Can you spot the problem? 你能发现问题吗? The behavior is that it varies in speed despite being normalized, which i'm not understanding. 行为是尽管标准化,但速度仍在变化,我不了解。

Script where direction calculation happens: 进行方向计算的脚本:

    public void RotateMouse()
{
    if (Input.GetButton(FindCharacter.FindPlayer(gameObject) + "AimPC"))
    {
        aimingMouse = true;
        sRenderer.enabled = true;
        canShoot = true;

        var mousePos = Input.mousePosition;
        var magePos = Camera.main.WorldToScreenPoint(transform.position);
        difference = mousePos - magePos;

        difference.Normalize(); // after normalize == direction

        rotation_z = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(new Vector3(transform.rotation.x, transform.rotation.y, rotation_z + spriteRotationOffset));

    }
    else
    {
        aimingMouse = false;
        sRenderer.enabled = false;
        canShoot = false;
    }

}

Another script where i instantiate the projectile and immediately set the direction after: 我实例化弹丸并立即设置方向的另一个脚本:

projectileDir = new Vector3(controller.difference.x, controller.difference.y, controller.difference.z); projectileDir =新的Vector3(controller.difference.x,controller.difference.y,controller.difference.z);

    public IEnumerator ShootProjectile(int elementNr)
{
    shooting = true;

    GameObject projectile = Instantiate(projectilePrefabs[elementNr], instantiatePosition, Quaternion.Euler(0, 0, controller.rotation_z + rotationOffsetZ));
    projectile.GetComponent<ProjectileMovement>().Direction(projectileDir);
    yield return new WaitForSeconds(shootDuration);

    shooting = false;

}

And lastly where the direction variable gets used and with calculating the speed: 最后,使用方向变量并计算速度:

public class ProjectileMovement : MonoBehaviour {

public float speed;

private Rigidbody2D rb;
private Vector3 direction;
private Collider2D projectileCollider;
private Animator animator;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
    projectileCollider = GetComponent<Collider2D>();
    animator = GetComponent<Animator>();
    rb.velocity = speed * direction;
}

public void Direction(Vector3 startingDirection)
{
    this.direction = startingDirection;
}

void FixedUpdate()
{
    rb.velocity = direction * speed;

}
}

And also, if you want more information about the scripts i can provide that, but i think i am showing everything involved with this problem. 而且,如果您想了解有关脚本的更多信息,我可以提供,但是我想我正在展示与此问题有关的所有内容。 If something is unclear, then you can comment it. 如果不清楚,则可以对其进行评论。

If the problem is hard to visualize, look at this GIF and observe the fireball velocity relative to the mousepointer: 如果问题很难发现,请查看此GIF并观察火球相对于鼠标指针的速度:
Gyazo gif link Gyazo gif链接

问题是我对Vector3进行了归一化,并通过首先对Vector2进行归一化(这给了我不同的/正确的结果)并随后将x和y值放入Vector3中来利用z维度来解决了问题!

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

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