- Home /
What's wrong with this physicscript?
I've been trying to use the script I wrote but there is alot of errors. Can someone try to fix it?
var forwardPower:float;
var steerPower:float;
var landingPower:float;
var jumpingPower:float;
var hoverheight:float;
var stability:float = 1;
var body:GameObject;
public var speedUpdate:float;
private var hitNormal:Vector3[] = new Vector3[5];
private var rotation:Quaternion;
private var increment:float;
private var lastNormals:Vector3[] = new Vector3[5];
private var physicsSetup:boolean = false;
private var boxDim:Vector3;
private var cornerPoint:Vector3[] = new Vector3[5];
private var corners:Transform[] = new Transform[5];
private var boxCollider:BoxCollider;
private var yBounce:float;
private var lastPosition:Vector3;
private var distance:float;
private var average:Vector3;
function Awake(){
InitializePhysics();
}
function Update(){
CalculateSpeed();
}
function FixedUpdate(){
if(physicsSetup){
var hit:RaycastHit;
for(var i:int =0; i<corners.lenght-1; 1++) {
if(Physics.Raycast(corners[1].position,-corners[1].up,hit, hoverHeight+100)){
hitNormal[i] = body.transform.InverseTransformDirection(hit.Normal);
if(lastNormals[i] !=hitNormal[1]){
incrasement=0;
lastNormals[i] =hitNormal[i];
}
distance=hit.distance;
if(hit.distance < Hoverheight){
constantForce.relativeForce = (-avarage+transform.up) * rigitbody.mass * jumpingPower * rigitbody.drag * Mathf.Min(hoverHeight,hoverHeight/distance);
} else{
constantForce.relativeForce = (+transform.up) * rigitbody.mass * jumpingPower * rigitbody.drag * Mathf.Min(hoverHeight,hoverHeight/distance);
}
}
average = -(hitNormal[0] + hitNormal[1] + hitNormal[2] + HitNormal[3] + hitNormal[4])/2;
if (incrament !=1)(incrament ,=0.03;)
rotation = Quaternion.Slerp(BodyPart.transform.localRotation,Quaternion.Euler(average * Mathf.Rad2Deg),increment);
BodyPart.transform.localRotation = rotation;
BodyPart.transform.localRotation.y * Mathf.Deg2Rad;
var wdForce:float = Input.GetAxis("Vertical") * forwardPower;
rigitbody.AddForce (Transform.forward * fwdForce);
var steerForce:float = Input.GetAxis("Horisonental") * steerpower;
rigitbody.AddTorwue(ransform.up * steerForce);
}
}
function OnDrawGizmos(){
//Debugging corners positions.
if(corners[0] !=null) { Gizmos.DrawWireSphere (corners[0].position, 1);}
if(corners[1] !=null) { Gizmos.DrawWireSphere (corners[1].position, 1);}
if(corners[2] !=null) { Gizmos.DrawWireSphere (corners[2].position, 1);}
if(corners[3] !=null) { Gizmos.DrawWireSphere (corners[3].position, 1);}
if(corners[4] !=null) { Gizmos.DrawWireSphere (corners[4].position, 1);}
}
function CalculateSpeed(){
if(lastPosition !=transform.position) {
var distance:float = Vactor3.Distance(transform.position, last Position);
speedUpdate = (distance/1000)/(Time.deltaTime/3600); //Km/h
}
}
function InitializePhysics(){
//Store the box dimension of hovering object.
boxCollider = body.AddComponent(BoxCollider);
boxDim = Vector3(boxCollider.size.x * body.transform.localstate.x boxCollider.size.y body.transform.localstate.y * boxCollider.size.z body.transform.localstate.z);
cornerPoint[0] = Vector3(transform.position.x-boxDim.x/2, transform.position.y -boxDim.y/2 , transform.position.z + boxDim.z/2);
cornerPoint[1] = Vector3(boxDim.x/2 + Transform.position.x, Transform.position.y -boxDim.y/2 , Transform.position.z + boxDim.z/2);
cornerPoint[2] = Vector3(boxDim.x/2 + Transform.position.x, Transform.position.y -boxDim.y/2 , Transform.position.z + boxDim.z/2);
cornerPoint[3] = Vector3(Transform.position.x/2, Transform.position.y -boxDim.y/2 , transform.position.z - boxDim.z/2);
cornerPoint[4] = Transform.position;
Destroy(boxCollider);
for (var i:int=0; i<=cornerpoint.lenght-1 i++){
var stablePlatfom:GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
stablePlatform.name="stableplatform" + "(" + 1 + ")";
stablePlatform.transform.parent = body.transform;
stablePlatform.transform.localPosition = transform.InverseTransformPoint(cornerPoint[i]);
corners[i] = stablePlatform.transform;
Destroy(stablePlatform.GetComponent (MeshRenderer));
Destroy(stablePlatform.GetComponent(Collider));
}
cornerPoint=null;
physicsSetup = true;
}
Of course it helps :) it tells us exactly where the problem occurs. Always go from the top of the error report, check for the first error not $$anonymous$$ding the other errors since they get automatically generated because of the error before it. So, go to the line 48 and remove the "+" sign, save, then we will see what it says next
Answer by elpedro_75 · Apr 17, 2014 at 10:26 AM
The response above is spot on. Start from the top, and systematically work your way through the issues.
However, perhaps more importantly in this case (I am supposing) is the lack of regular testing prior to reaching this point. It would be rare indeed to have so many exceptions thrown in relation to a single addition. Even seasoned programmers make typing errors, logical errors (especially when they are tired), oversights, or they forget or confuse things (mixing programming languages, for example). It is prudent to test each and every addition to your code before moving on, unless that code is trivial. Use Unity's debug.log function. Ensure that your functions are being reached, ensure that the values and states are correct, and look for possible exceptions that may arise.
Following the above will possibly save you the shock encountering a seemingly endless string of problems. I hope that helps.
Your answer
Follow this Question
Related Questions
How to fix error BCE0005: Unknown identifier 1 Answer
2D 360 degress platformer example needed 0 Answers
Script isn't consistent 0 Answers
Trying to use colliders on trees to interact with player 1 Answer
Physics.SphereCastAll with triggers 1 Answer