GameObject transform.Rotate() [英] GameObject transform.Rotate()

查看:164
本文介绍了GameObject transform.Rotate()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.我在屏幕上有4个对象,并有一个弹丸,如下图所示. 图片来源

I have a question. I have 4 objects on the screen and a projectile as in the picture below. image source

当我单击4弹丸中的对象时,它会更改位置,指示我单击的对象. 这是使用的代码,但是不起作用.

When I click on an object in the 4 projectile it changes position indicating to the object I clicked on. This is the code used but it does not work.

public GameObject Tun;
public GameObject[] robotColliders;
public GameObject[] Robots;

 foreach(GameObject coll in robotColliders)
    {
        coll.GetOrAddComponent<MouseEventSystem>().MouseEvent += SetGeometricFigure;
    }

   private void SetGeometricFigure(GameObject target, MouseEventType type)
{
    if(type == MouseEventType.CLICK)
    {
        Debug.Log("Clicked");
        int targetIndex = System.Array.IndexOf(robotColliders, target);
        Tun.transform.DORotate(Robots[targetIndex].transform.position, 2f, RotateMode.FastBeyond360).SetEase(Ease.Linear);
    }
}

我当时正在考虑使用组件DORotate(),但是它仍然无法正常工作.有人知道如何解决此问题吗?

I was thinking about using the component DORotate(), But it does not work anyway. Does anyone know how to fix this problem?

推荐答案

一种方法是使用四元数来设置绕z轴的旋转,并使用对象和箭头之间的矢量来获取角度./p>

One method would be to use a quaternion to set the rotation about the z-axis, using a vector between the object and your arrow to get the angle.

Vector2 dir = Robots[targetIndex].transform.position - Tun.transform.position;
Tun.transform.rotation = Quaternion.Euler(0, 0, Mathf.atan2(dir.y, dir.x)*Mathf.Rad2Deg - 90); // may not need to offset by 90 degrees here;

这篇关于GameObject transform.Rotate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆