- Home /
Trailer/wagon Physics behaving weirdly
I am working on 2d trailer game. In this game I am attaching the trailer with truck by hinge joint. My problem is the forward movement of truck and trailer is fine but the trailer drags on its backward turning movement. Please refer the below script for the truck movement and the youtube video link:-
https://www.youtube.com/watch?v=cKGxuqov_jI&feature=youtu.be
using UnityEngine; using System.Collections; public class PlayerVehicle : MonoBehaviour { const float kTiltSensitivityScale = 10.0f; //IJS_CHANGES on 02/1/14 // ----------- CAR TUTORIAL----------------- // These variables allow the script to power the wheels of the car. public float startingY; // public WheelCollider FrontLeftWheel; // public WheelCollider FrontRightWheel; // public WheelCollider BackLeftWheel; // public WheelCollider BackRightWheel; public GameObject frontLeftWheel; public GameObject frontRightWheel; public GameObject frontLeftWheelSprite; public GameObject frontRightWheelSprite; public GameObject VehicleSprite; public GameObject autoRepairBar; public tk2dSprite cargoSprite; //public GameObject smokeParticleEmmiter; public float startingHealth; public float autoRepairSwingLength; private float health; private bool VehicleMovementOn; // These variables are just for applying torque to the wheels based on // the maximum forward rpm and maximum reverse rpm. // public float EngineTorque = 230.0f; // public float MaxForwardRPM = 400.0f; // public float MaxRevRPM = 200.0f; // public float maximumTurn; // public float turnSpeed; // private float EngineRPM = 0.0f; public float forwardAcceleration = 450.0f ; public float reverseAcceleration = 400.0f; public float maxForwardSpeed = 700.0f; public float maxReverseSpeed = 450.0f; public float maxTurnAngle; public float angleLimit=180f; public float steeringBounceBackSpeed=100f; public AudioSource ignitionClip; public AudioSource engineClip; public AudioSource crashClip; public AudioSource levelCompleteSound; public float minimumDamageSpeed; private float lastHealthShrinkTime; private float bufferTimeForNextHealthShrink=0.5f; private float gas; private float turn; private DrivingWheelControl steeringWheel; private GasSliderControl slidingBar; private GameObject smokeParticles; [HideInInspector] public GameObject animationObject; // games2win code driving game objects // private bool enableTuning; public static float vehicleSpeed; public static Vector3 location; private float clampedMaxForwardSpeed; private float startingAutoRepairBarX; private bool startAutoRepairSwinging; private int autoRepairSwingDirection; private float autoRepairSwingSpeed=1f; private float collisionEnterSpeed; private bool engineIgnited; private float ignitionStartTime; private GameObject tempGO; //IJS_CHANGES on 30/12/13 private bool isTiltControl; private float tiltSensitivity; private bool isCalibrated { get { return (PlayerPrefs.GetInt("isCalibrated") == 1); } set { PlayerPrefs.SetInt("isCalibrated", value ? 1 : 0); } } private ScreenOrientation calibratedOrinetation; private Vector3 calibratedAcceleration; private float turnAngle; private Vector3 turnAxis; public bool TiltControl { get { return (isTiltControl); } set { isTiltControl = value; if(steeringWheel) { steeringWheel.SetEnabled( !isTiltControl); } } } public float TiltSensitivity { get { return (tiltSensitivity); } set { tiltSensitivity = value; } } public float ReturnVehicleSpeed() { return gameObject.rigidbody.velocity.sqrMagnitude; } void Start () { transform.position=new Vector3(transform.position.x,startingY,transform.position.z); transform.rigidbody.velocity=new Vector3(0f,0f,0f); VehicleMovementOn=true; health=startingHealth; lastHealthShrinkTime=0.0f; VehicleSprite.GetComponent<tk2dSprite>().SetSprite("1"); clampedMaxForwardSpeed=maxForwardSpeed; startingAutoRepairBarX= autoRepairBar.transform.localPosition.x; startAutoRepairSwinging=false; engineIgnited=false; autoRepairSwingDirection=1; // alter the center of mass to make the vehicle more stable. // gameObject.rigidbody.centerOfMass = new Vector3(0f, -0.75f, 0f); //IJS_CHANGES on 31/12/13 TiltControl = PlayerPrefs.GetString("ControlMode").Equals("Accelerometer"); TiltSensitivity = PlayerPrefs.GetFloat(SettingsControl.kAccelerometerSensitivityKey); } void Calibrate() { calibratedOrinetation = Screen.orientation; calibratedAcceleration = Input.acceleration; isCalibrated = true; } void FixedUpdate() { if (VehicleMovementOn&&animationObject==null) { #if UNITY_ANDROID || UNITY_IPHONE //IJS_CHANGES on 02/01/14 if( !steeringWheel) { tempGO = GameObject.Find ("SteeringWheel"); if(tempGO) { steeringWheel = tempGO.GetComponent<DrivingWheelControl>(); steeringWheel.angleLimit=angleLimit; steeringWheel.bounceBackSpeed = steeringBounceBackSpeed; steeringWheel.SetEnabled( !isTiltControl); } } //IJS_CHANGES on 30/12/13 if(TiltControl) { turnAxis = Vector3.Cross(Input.acceleration, new Vector3(0,0,-1));//calibratedAcceleration turnAngle = Vector3.Dot(-Vector3.up, turnAxis); turnAngle = (turnAngle > 180f) ? (turnAngle - 360f) : turnAngle; turnAngle = (turnAngle < -180f) ? (turnAngle + 360f) : turnAngle; turn = Mathf.Clamp((TiltSensitivity * kTiltSensitivityScale * -turnAngle), -1f, 1f); } else { if(steeringWheel) { turn=-steeringWheel.GetTurn(); } } if(slidingBar) { gas=slidingBar.GetGas(); } else { tempGO = GameObject.Find ("SliderBar"); if(tempGO) { slidingBar=tempGO.GetComponent<GasSliderControl>(); } } #endif #if UNITY_EDITOR gas=Input.GetAxis("Vertical"); turn=Input.GetAxis("Horizontal"); #endif if(engineIgnited==false && Mathf.Abs(gas)>0.0001f && Time.timeScale>0.5f) { engineIgnited=true; ignitionClip.Play(); engineClip.Play(); engineClip.pitch= 0.5f; ignitionStartTime=Time.time; } if(Time.time<ignitionStartTime+0.75f) { gas=0f; } AccelerateStuff(); SetWheelTurnAndSpeed(frontLeftWheel,turn,gas); SetWheelTurnAndSpeed(frontRightWheel,turn,gas); float angle= frontLeftWheel.transform.localEulerAngles.y; frontLeftWheelSprite.transform.localEulerAngles= new Vector3(0,angle*2,0); frontRightWheelSprite.transform.localEulerAngles= new Vector3(0,angle*2,0); if(engineClip.isPlaying && Time.time>ignitionStartTime+0.5f) { engineClip.pitch= 0.5f + Mathf.Clamp01(gas)*0.5f; } } location=transform.position; if(startAutoRepairSwinging) { autoRepairBar.transform.localPosition += new Vector3(Time.deltaTime*autoRepairSwingDirection* autoRepairSwingSpeed * 2f , 0f,0f); // sathya increased autorepair speed. Added *2f on 16/12/13 if(autoRepairBar.transform.localPosition.x>startingAutoRepairBarX+autoRepairSwingLength) { autoRepairBar.transform.localPosition=new Vector3(startingAutoRepairBarX+autoRepairSwingLength,autoRepairBar.transform.localPosition.y, autoRepairBar.transform.localPosition.z); autoRepairSwingDirection=-1; } if(autoRepairBar.transform.localPosition.x<startingAutoRepairBarX) { startAutoRepairSwinging=false; autoRepairBar.renderer.enabled=false; autoRepairSwingDirection=1; SetHealth(Mathf.Clamp(health + 1f, -1f, 5f)); } } } private void AccelerateStuff () { float vel = 0.0f; if (gas>0) { vehicleSpeed += Time.deltaTime * forwardAcceleration * gas; if(gas>0.25f)maxForwardSpeed = clampedMaxForwardSpeed*gas; } else if(gas<0) { vehicleSpeed += Time.deltaTime * reverseAcceleration * gas; } else { vehicleSpeed = Mathf.SmoothDamp(vehicleSpeed, 0.0f, ref vel, Time.deltaTime * 8.0f); //vehicleSpeed *= 0.89f; } vehicleSpeed= Mathf.Clamp(vehicleSpeed,-maxReverseSpeed,maxForwardSpeed); if (Mathf.Abs (gas) < 0.1) { vehicleSpeed = Mathf.SmoothDamp(vehicleSpeed, 0.0f, ref vel, Time.deltaTime * 8.0f); //vehicleSpeed *= 0.89f; } } void OnCollisionEnter(Collision collision) { // print ("collided with "+collision.gameObject); int collisionLayer= collision.gameObject.layer ; if((collisionLayer == 11 ||collisionLayer== 12 ) && health>0) { //rigidbody.velocity= Vector3.zero; rigidbody.velocity = -rigidbody.velocity; vehicleSpeed = 0.0f; if (collisionLayer == 12) { collision.collider.rigidbody.velocity = Vector3.zero; } collisionEnterSpeed=collision.relativeVelocity.magnitude; if(collisionEnterSpeed>minimumDamageSpeed&&Time.time>lastHealthShrinkTime+bufferTimeForNextHealthShrink) { health-=1f; lastHealthShrinkTime=Time.time; SetVehicleSprite(health); if(crashClip.isPlaying==false) { crashClip.pitch = 0.5f+ 0.5f* Mathf.Clamp01(0.5f*(collisionEnterSpeed-minimumDamageSpeed)); crashClip.Play(); } } } } void OnCollisionStay () { //rigidbody.velocity = Vector3.zero; vehicleSpeed = 0.0f; } public void SetHealth(float h) { health=h; SetVehicleSprite(h); } public void SetHealth() { SetVehicleSprite(health); } public float GetHealth() { return(health); } public void SetCargoSprite(string spriteName) { if(cargoSprite!=null) { if(spriteName=="none") { cargoSprite.gameObject.renderer.enabled=false; } else { cargoSprite.gameObject.renderer.enabled=true; cargoSprite.SetSprite(spriteName); } } } public void SetVehicleSprite(float health) { if(health>4.75f) { VehicleSprite.GetComponent<tk2dSprite>().SetSprite("1"); } else if(health>3f) { VehicleSprite.GetComponent<tk2dSprite>().SetSprite("2"); } else if(health>2f) { VehicleSprite.GetComponent<tk2dSprite>().SetSprite("3"); } else if(health>1f) { VehicleSprite.GetComponent<tk2dSprite>().SetSprite("4"); } else if(health>0f) { VehicleSprite.GetComponent<tk2dSprite>().SetSprite("5"); } else if(health<-0.5f) { VehicleSprite.GetComponent<tk2dSprite>().SetSprite("7"); } else { VehicleSprite.GetComponent<tk2dSprite>().SetSprite("6"); } if(health<4.75f && health>0.25f) { Invoke("InitiateAutoRepair", 0.5f); } if(health<0.5f) { FadeEngineSound(); } } public void FadeEngineSound() { iTween.AudioTo(gameObject, iTween.Hash("audiosource",engineClip,"pitch",0.5f,"volume",0f,"time",1f, "easetype","linear","ignoretimescale", true)); } public void LevelCompleteSounds() { FadeEngineSound(); } public void PlayCheckPointReachedSound() { levelCompleteSound.Play(); } public void LevelFailedSounds() { FadeEngineSound(); } void InitiateAutoRepair() { if(startAutoRepairSwinging==false && PlayerPrefs.GetInt("AutoRepairOn") == 1) { if(PlayerPrefs.GetInt("UnlimitedAutoRepair")==1) { print ("doing autoRepair"); autoRepairBar.renderer.enabled=true; autoRepairSwingDirection=1; startAutoRepairSwinging=true; } else if(PlayerPrefs.GetInt("AvailableAutoRepairs")>0) { print ("doing autoRepair"); autoRepairBar.renderer.enabled=true; autoRepairSwingDirection=1; startAutoRepairSwinging=true; PlayerPrefs.SetInt("AvailableAutoRepairs", PlayerPrefs.GetInt("AvailableAutoRepairs")-1); PlayerPrefs.Save(); } } } public void ToggleVehicleMovementOnState(bool state) { VehicleMovementOn=state; } void SetWheelTurnAndSpeed(GameObject wheel,float turn, float speed) { Vector3 localVelocity = wheel.transform.InverseTransformDirection (wheel.rigidbody.velocity); localVelocity = new Vector3(localVelocity.x,0,localVelocity.z); wheel.rigidbody.velocity = wheel.transform.TransformDirection (localVelocity); wheel.transform.localRotation=Quaternion.Euler (new Vector3 (0, turn*maxTurnAngle, 0)); if (wheel.transform.localEulerAngles.y < 180) { if (wheel.transform.localEulerAngles.y > maxTurnAngle) { wheel.transform.localEulerAngles = new Vector3 (0, maxTurnAngle, 0); } } else { if (wheel.transform.localEulerAngles.y < 360-maxTurnAngle) { wheel.transform.localEulerAngles = new Vector3 (0, 360-maxTurnAngle, 0); } } wheel.rigidbody.velocity = wheel.transform.right *vehicleSpeed * Time.deltaTime; } //IJS_CHANGES on 29-01-2014 private bool toggleAccelerationSlider = false;
}`
Any kind of help is appreciated. Thanks
Your answer
Follow this Question
Related Questions
2D Joint/Wheel Question 1 Answer
Endless Runner Car Physics Issues (Again) 1 Answer
Cant seem to make this basic 3D car turn properly 0 Answers
Braketorque makes car behave like a kinematic rigidbody 0 Answers
Car turn back after release button 0 Answers