简体   繁体   English

Unity5 C#:如何通过触摸拖动旋转GameObject

[英]Unity5 C#: How to rotate a GameObject with touch drag

I have a cube in my scene which I want to to rotate as player touch and drag it. 我的场景中有一个立方体,我想在玩家触摸并拖动它时旋转。 here is the code 这是代码

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast (ray, out hit);

if(Input.GetMouseButton(0) &&  hit.collider.name == "Cube")
{
    xDeg -= Input.GetAxis("Mouse X") * speed * friction;
    yDeg += Input.GetAxis("Mouse Y") * speed * friction;
    fromRotation = cube.transform.rotation;
    toRotation = Quaternion.Euler(yDeg,xDeg,0);
    cube.transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime  * lerpSpeed);
}

I am 100% able to do that, problem is I have placed several other game objects with colliders on each wall of cube. 我100%能够做到这一点,问题是我在立方体的每面墙上都放置了其他带有碰撞器的游戏对象。 All I want is player to be able to rotate cube around and perform some action by tapping on specific game objects on each wall. 我想要的是玩家能够旋转立方体并通过点击每堵墙上的特定游戏对象来执行一些操作。 here is the rest of code 这是其余的代码

if (Input.GetMouseButtonUp(0))
{
    Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast (ray1, out hit)) {
        if (hit.collider.name == "ABC") {
            //Perform action specific to ABC
        }   
     }
 }

Now if I try rotating the cube it runs ABC (because definitely rayCast work it suppose to be). 现在,如果我尝试旋转多维数据集,它将运行ABC(因为rayCast确实可以实现它的工作)。 I can't figure out how I actually achieve this that player became able to rotate the cube by touch and drag as well as perform some action by just tapping on walls of same cube. 我无法弄清楚玩家是如何通过触摸和拖动来旋转多维数据集以及仅通过敲击相同多维数据集的墙壁来执行某些操作的,实际上是如何实现的。

Put your cube collider in a different layer than the other objects. 将立方体碰撞器放置在与其他对象不同的层中。 Then call the version of Physics.Raycast that takes a layerMask. 然后调用带有layerMask的Physics.Raycast版本。

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

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