在Unity3D中滚动刚体立方体

时间:2016-04-24 16:04:46

标签: javascript unity3d

所以在Unity3D中我有一个由玩家控制的立方体(使用箭头)。玩家可以随心所欲地去任何他想去的地方。问题是,当我向立方体添加刚体时,它不再那么光滑,不知何故最后它只是毛刺。试图与torque玩一点,但这没有帮助,或者我还不清楚如何正确实现它。这是我用于多维数据集的代码(它是用JavaScript编写的)。任何帮助将不胜感激,即使你不知道一个想法仍然重要的JavaScript。谢谢。代码:

private var ismoving : boolean = false;  
private var startY : float = 0;  
var cubeSpeed : float;  
var cubeSize : float;
var jumpHeight = 3.0;


function Update ()   
{  

    if (Input.GetKeyDown("up") && ismoving == false)  
    {  
        ismoving = true;  
        transform.Find("targetpoint").Translate(0, -cubeSize/2 , cubeSize/2);  
        StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.right, 90.0f,cubeSpeed));  

    }   
    if (Input.GetKeyDown("down") && ismoving == false)  
    {  
        ismoving = true;  
        transform.Find("targetpoint").Translate(0, -cubeSize/2, -cubeSize/2);  
        StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.right, 90.0f,cubeSpeed));  
    }  
    if (Input.GetKeyDown("left") && ismoving == false)  
    {  
        ismoving = true;  
        transform.Find("targetpoint").Translate(-cubeSize/2, -cubeSize/2, 0);  
        StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.forward, 90.0,cubeSpeed));  
    }  
    if (Input.GetKeyDown("right") && ismoving == false)  
    {  
        ismoving = true;  
        transform.Find("targetpoint").Translate(cubeSize/2, -cubeSize/2, 0);  
        StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.forward, 90.0f,cubeSpeed));     
    }  
        if (Input.GetKeyDown("space") && ismoving == false)  
    {  

    GetComponent.<Rigidbody>().AddForce(new Vector3(0, jumpHeight, 0), ForceMode.Impulse);
    } 



}  

function DoRoll (aPoint, aAxis, aAngle, aDuration) {    

 var tSteps = Mathf.Ceil(aDuration * 30.0);  
 var tAngle = aAngle / tSteps;  
 var pos : Vector3; 

  for (var i = 1; i <= tSteps; i++)   
  {   
    transform.RotateAround (aPoint, aAxis, tAngle);  
    yield WaitForSeconds(0.0033333);  
  }   


   transform.Find("targetpoint").position = transform.position;  


   pos = transform.position;  
   pos.y = startY;  
   transform.position = pos;  


   var vec = transform.eulerAngles;  
   vec.x = Mathf.Round(vec.x / 90) * 90;  
   vec.y = Mathf.Round(vec.y / 90) * 90;  
   vec.z = Mathf.Round(vec.z / 90) * 90;  
   transform.eulerAngles = vec;  


   ismoving = false;       
}

 function FixedUpdate () {

}  

1 个答案:

答案 0 :(得分:4)

它可能无法顺利移动,因为您正在使用转换修改其位置和轮换var elements = document.querySelectorAll('.UFILikeLink'); for (var i = 0; i < elements.length; i++) { var element = elements[i]; element.innerHTML = element.innerHTML.replace(/Like/g, 'ORly?'); } ,而不是使用directly函数。

附加Rigidbody后,不要使用Rigidbodytransform.position = pos;;

transform.eulerAngles = vecRigidbody.MovePositionRigidbody.MoveRotationRigidbody.AddForce是您应该使用的功能。

Rigidbody.AddTorque功能

中替换这些内容
DoRoll