Hi all, I have some problems regarding the usage of DontGoThroughScript.js. When I attach the script to my model, It still run through the collisions.
Then I try to include the codes within the control script for the character’s movement, and the problem still the same. Here is my script:
var spd = 0;
var layerMask : LayerMask;
var skinWidth : float;
var minExt : float;
var partialExt : float;
var sqrMinExt : float;
var previousPos : Vector3;
var body : Rigidbody;
function Awake() {
body = rigidbody;
previousPos = body.position;
skinWidth = 0.1;
minExt = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z);
partialExt = minExt*(1.0-skinWidth);
sqrMinExt = minExt*minExt;
}
function FixedUpdate() {
var movement : Vector3;
var movementSqr : float;
movement = body.position - previousPos;
movementSqr = movement.sqrMagnitude;
if (movementSqr > sqrMinExt) {
var movementMag : float;
var hit : RaycastHit;
movementMag = Mathf.Sqrt(movementSqr);
if (Physics.Raycast (previousPos, movement, hit, movementMag, layerMask.value)) {
body.position = hit.point-(movement*movementMag)*partialExt;
}
}
previousPos = body.position;
var dir : Vector3 = Vector3.zero;
dir.z = Input.acceleration.x + 0.7;
if (Input.acceleration.y <= -0.2 && Input.acceleration.y >= -1){
transform.Rotate(0,-Input.acceleration.y*2,0);
} else if (Input.acceleration.y >= 0.2 && Input.acceleration.y <= 1){
transform.Rotate(0,-Input.acceleration.y*2,0);
} else {
transform.Rotate(0,0,0);
}
if (Input.acceleration.x <= -0.8){
spd = 0;
animation.Play("idle");
} else if (Input.acceleration.x > -0.8 && Input.acceleration.x <= -0.5){
Debug.Log("walk");
spd = 5;
animation.CrossFade("walk");
} else if (Input.acceleration.x > -0.5 && Input.acceleration.x <= -0.2){
Debug.Log("run");
spd = 10;
animation.CrossFade("run");
} else if (Input.acceleration.x > -0.2){
spd = 15;
Debug.Log("sprint");
animation.CrossFade("run");
}
if (dir.sqrMagnitude > 1) {
dir.Normalize();
}
dir *= Time.deltaTime;
transform.Translate (dir * spd);
Debug.Log(dir);
}
Thanks in advance!