- Home /
Other
Boat script problems when changing platform to android
OK so Ive been working on some kind of boat android project thingy. i have unity remote on my galaxy s3 so instead of building and deploying the app i would just run it like that. but i kept the platform on PC. the script worked perfectly and the touch controls were decent. but when i change the platform to android i get a sea of errors. i fixed some of them simply by putting a var before some lines but the others i cant fix. can you please correct the errors in my script. thanks in advance. PS: i would put the error codes in but there's so many of them i don't know where to start. if you all have unity pro and a at least basic android license please give it a try to see all of the errors.
//Mass
var mass = 3000;
//Force of the boats engine
var engineForce = 10000.0;
//Rudder torque coefficient for steering the boat
var rudder = 40;
//How far the direction of the propeller force is deflected by the rudder
var propellerTurningAngle = 20;
//drag coefficients along x,y and z directions
var drag = Vector3(6.0,4.0,0.2);
//angular drag coefficient
var angularDrag = 0.8;
//heigh of center of gravity
var cogY = -0.5;
//max width, height and length of the boat (used for water dynamics)
var size = Vector3(3,3,10);
//volume of boat in liters (the higher the volume, the higher the boat will floar)
var volume = 9000;
//particle system used for foam from the boat's propeller
var engineSpume : Transform;
var throttle : GUITexture;
var reverse : GUITexture;
var left : GUITexture;
var right : GUITexture;
private var queryUserInput = true;
private var rpmPitch = 0.0;
private var waterSurface = null;
function Start()
{
//Destroy existing rigidbody, we don't want anyone to mess with it.
if(rigidbody)
Destroy(rigidbody);
//setup rigidbody
gameObject.AddComponent(Rigidbody);
rigidbody.mass = mass;
rigidbody.angularDrag = angularDrag;
rigidbody.centerOfMass.y = cogY;
rigidbody.interpolation = RigidbodyInterpolation.Interpolate;
//start engine noise
audio.loop = true;
audio.Play();
//check for particle emitter
if(engineSpume != null)
if(engineSpume.particleEmitter == null)
{
Debug.Log("The Engine Spume GameObject needs to have a ParticleEmitter Component!");
engineSpume = null;
}
if(GetComponentInChildren(Collider) == null)
Debug.Log("The Boat needs a collider to float on the water!");
}
//Functions to be used by external scripts
//controlling the boat if required
//===================================================================
//return a status string for the vehicle
function GetStatus(status : GUIText) {
status.text="v="+(rigidbody.velocity.magnitude * 3.6).ToString("f1") + " km/h";
}
//return an information string for the vehicle
function GetControlString(info : GUIText) {
info.text="Use arrow keys to control the boat.";
}
//Setup main camera to follow boat
function SetupCamera() {
if(Camera.main.GetComponent(SmoothFollow) != null)
{
Camera.main.GetComponent(SmoothFollow).enabled=true;
Camera.main.GetComponent(SmoothFollow).target=transform;
Camera.main.GetComponent(SmoothFollow).distance=11;
Camera.main.GetComponent(SmoothFollow).height=3;
}
Camera.main.transform.parent=null;
}
//Enable or disable user controls
function SetEnableUserInput(enableInput)
{
queryUserInput=enableInput;
}
//Boat physics
//======================================================================
function FixedUpdate () {
//if there is no water surface we are colliding with, no boat physics
if(waterSurface==null)
return;
//query input axes if necessarry
motor = 0.0;
steer = 0.0;
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Stationary && throttle.HitTest (touch.position)) {
motor = 1;
}
else if (touch.phase == TouchPhase.Ended && throttle.HitTest (touch.position)) {
motor = 0;
}
if (touch.phase == TouchPhase.Stationary && reverse.HitTest (touch.position)) {
motor = -.5;
}
else if (touch.phase == TouchPhase.Ended && reverse.HitTest (touch.position)) {
motor = 0;
}
if (touch.phase == TouchPhase.Stationary && left.HitTest (touch.position)) {
steer = -1;
}
else if (touch.phase == TouchPhase.Ended && left.HitTest (touch.position)) {
steer = 0;
}
if (touch.phase == TouchPhase.Stationary && right.HitTest (touch.position)) {
steer = 1;
}
else if (touch.phase == TouchPhase.Ended && right.HitTest (touch.position)) {
steer = 0;
}}
//if(queryUserInput)
//{
//motor = Input.GetAxis("Vertical");
//steer = Input.GetAxis("Horizontal");
//}
//get water level and percent under water
waterLevel=waterSurface.collider.bounds.max.y;
distanceFromWaterLevel = transform.position.y-waterLevel;
percentUnderWater = Mathf.Clamp01((-distanceFromWaterLevel + 0.5*size.y)/size.y);
//Buoyancy (the force which keeps the boat floating above water)
//--------------------------------------------------------------
//the point the buoyancy force is applied onto is calculated based
//on the boat's picth and roll, so it will always tilt upwards:
buoyancyPos=transform.TransformPoint(-Vector3(transform.right.y*size.x*0.5,0,transform.forward.y*size.z*0.5));
//then it is shifted arcording to the current waves
buoyancyPos.x+=waterSurface.waveXMotion1*Mathf.Sin(waterSurface.waveFreq1*Time.time)
+waterSurface.waveXMotion2*Mathf.Sin(waterSurface.waveFreq2*Time.time)
+waterSurface.waveXMotion3*Mathf.Sin(waterSurface.waveFreq3*Time.time);
buoyancyPos.z+=waterSurface.waveYMotion1*Mathf.Sin(waterSurface.waveFreq1*Time.time)
+waterSurface.waveYMotion2*Mathf.Sin(waterSurface.waveFreq2*Time.time)
+waterSurface.waveYMotion3*Mathf.Sin(waterSurface.waveFreq3*Time.time);
//apply the force
rigidbody.AddForceAtPosition(- volume * percentUnderWater * Physics.gravity , buoyancyPos);
//Engine
//--------------------------------------------------------------
//calculate propeller position
propellerPos = Vector3(0,-size.y*0.5,-size.z*0.5);
propellerPosGlobal=transform.TransformPoint(propellerPos);
//apply force only if propeller is under water
if(propellerPosGlobal.y<waterLevel)
{
//direction propeller force is pointing to.
//mostly forward, rotated a bit according to steering angle
steeringAngle = steer * propellerTurningAngle * Mathf.Deg2Rad;
propellerDir = transform.forward*Mathf.Cos(steeringAngle) - transform.right*Mathf.Sin(steeringAngle);
//apply propeller force
rigidbody.AddForceAtPosition(propellerDir * engineForce * motor , propellerPosGlobal);
//create particles for propeller
if(engineSpume!=null)
{
engineSpume.position = propellerPosGlobal;
engineSpume.position.y = waterLevel-0.5;
engineSpume.particleEmitter.worldVelocity = rigidbody.velocity*0.5-propellerDir*10*motor+Vector3.up*3*Mathf.Clamp01(motor);
engineSpume.particleEmitter.minEmission = Mathf.Abs(motor)*3;
engineSpume.particleEmitter.maxEmission = Mathf.Abs(motor)*3;
engineSpume.particleEmitter.Emit();
}
}
//Drag
//--------------------------------------------------------------
//calculate drag force
dragDirection = transform.InverseTransformDirection(rigidbody.velocity);
dragForces = -Vector3.Scale(dragDirection,drag);
//depth of the boat under water (used to find attack point for drag force)
depth = Mathf.Abs(transform.forward.y)*size.z*0.5+Mathf.Abs(transform.up.y)*size.y*0.5;
//apply force
dragAttackPosition = Vector3(transform.position.x,waterLevel-depth,transform.position.z);
rigidbody.AddForceAtPosition(transform.TransformDirection(dragForces)*rigidbody.velocity.magnitude*(1+percentUnderWater*(waterSurface.waterDragFactor-1)),dragAttackPosition);
//linear drag (linear to velocity, for low speed movement)
rigidbody.AddForce(transform.TransformDirection(dragForces)*500);
//rudder torque for steering (square to velocity)
forwardVelo = Vector3.Dot(rigidbody.velocity,transform.forward);
rigidbody.AddTorque(transform.up*forwardVelo*forwardVelo*rudder*steer);
//Sound
//--------------------------------------------------------------
audio.volume=0.3+Mathf.Abs(motor);
//slowly adjust pitch to power input
rpmPitch=Mathf.Lerp(rpmPitch,Mathf.Abs(motor),Time.deltaTime*0.4);
audio.pitch=0.3+0.7*rpmPitch;
//reset water surface, so we have to stay in contact for boat physics.
waterSurface = null;
}
function OnTriggerStay(coll)
{
if(coll.GetComponent(FloatableWater)!=null)
waterSurface=coll.GetComponent(FloatableWater);
}
//Called by DamageReceiver if boat destroyed
function Detonate()
{
//no more boat force => sink
enabled=false;
//Mark object no longer a target for homing missiles.
if(tag=="MissileTarget")
tag="";
}
@script RequireComponent (AudioSource)
oh and its not really my script, i just put in the parts for the touch control
You should declare the type of your variables.
Also I think people could actually help you if you posted the errors, rather than just saying you get them!
UA is to help you understand your specific technical problems so you can solve your own problems. It is not for us to do your work for you by debugging a script largely written by someone else for you.