Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by piyushr · May 05, 2014 at 04:55 PM · physicscar

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges