2017-12-02 63 views
0

我剩下2個物體沒有剛體,右邊是剛性體(是Kinematic = false)。我可以通過左邊的和右邊的我可以移動。如何使角色不通過並移動對象?如何製作固定物體

沒有剛體

enter image description here

隨着剛體

enter image description here

enter image description here

enter image description here

enter image description here

using System.Collections; 
using UnityEngine; 

public class Person_controller : MonoBehaviour { 
public float rotationPeriod = 0.3f;  
public float sideLength = 1f;   

bool isRotate = false;     
float directionX = 0;     
float directionZ = 0;    

Vector3 startPos;      
float rotationTime = 0;     
float radius;       
Quaternion fromRotation;    
Quaternion toRotation;     

void Start() { 

radius = sideLength * Mathf.Sqrt (2f)/2f; 

} 

void Update() { 

float x = 0; 
float y = 0; 


x = Input.GetAxisRaw ("Horizontal"); 
if (x == 0) { 
    y = Input.GetAxisRaw ("Vertical"); 
} 


if ((x != 0 || y != 0) && !isRotate) { 
    directionX = y;               
    directionZ = x;                
    startPos = transform.position;            
    fromRotation = transform.rotation;           
    transform.Rotate (directionZ * 90, 0, directionX * 90,   Space.World);   
    toRotation = transform.rotation;            
    transform.rotation = fromRotation;           
    rotationTime = 0;  

    isRotate = true;                
    } 
} 

void FixedUpdate() { 

if (isRotate) { 

    rotationTime += Time.fixedDeltaTime;          
    float ratio = Mathf.Lerp(0, 1, rotationTime/rotationPeriod);   


    float thetaRad = Mathf.Lerp(0, Mathf.PI/2f, ratio);     
    float distanceX = (-directionX * radius * (Mathf.Cos (45f * Mathf.Deg2Rad) - Mathf.Cos (45f * Mathf.Deg2Rad + thetaRad)))*sideLength; 
    float distanceY = radius * (Mathf.Sin(45f * Mathf.Deg2Rad + thetaRad) - Mathf.Sin (45f * Mathf.Deg2Rad));      
    float distanceZ = directionZ * radius * (Mathf.Cos (45f * Mathf.Deg2Rad) - Mathf.Cos (45f * Mathf.Deg2Rad + thetaRad));   
    transform.position = new Vector3(startPos.x + distanceX, startPos.y + distanceY, startPos.z + distanceZ);      


    transform.rotation = Quaternion.Lerp(fromRotation, toRotation, ratio);  


    if (ratio == 1) { 
     isRotate = false; 
     directionX = 0; 
     directionZ = 0; 
     rotationTime = 0; 
    } 
    } 
} 
} 
+0

你爲什麼要添加一個「monogame」標籤?這似乎純粹是關於Unity – UnholySheep

+0

您是否還嘗試過僅向運動物體添加運動剛體(Kinematic設置爲true)? – UnholySheep

+0

是的,只是穿過物體 –

回答