1
\$\begingroup\$

I attached tilt script to my Player to move player x and y Axis. Player have Rigidbody2D and also IsKinematic=true. but the problem when the player can Die i want to stop tilting and player goes down to ground. here is my tilt Code

public float Speed=20f;
	void Update () {
		if (PlayerControll.instance.isAlive) {
			transform.Translate (Input.acceleration.x * Speed * Time.deltaTime, Input.acceleration.y * Speed * Time.deltaTime, 0);
		} else {
			PlayerControll.instance.PlayerRigidbody.isKinematic = false;
		}

I try to do that using disable tilt script when Player die and Iskinematic=false but the problem is player can freeze to his position not goes down to the ground. I do that so many way but not done so what i do don't know then i post the Question. thanks in advance.

\$\endgroup\$
1
\$\begingroup\$

You can disable a component (or even destroy it - it'll be removed from your gameObject) to tell Unity to stop calling its Update method.

To disable a component, from inside your tilt component, you can do this in the Update method:

if ( PlayerControll.instance.isAlive == false )
{    
    this.enabled = false;
}

To destroy your tilt component (remove it from your gameObject) you can do this in the Update method:

if ( PlayerControll.instance.isAlive == false )
{    
    Destroy(this);
}

Also, if I understand correctly, you want your player to go down by gravity. So, if it's not alive remember to check if the gravity is applied on the RigidBody doing something like this :

PlayerControll.instance.PlayerRigidbody.useGravity = true;
\$\endgroup\$
  • \$\begingroup\$ What is the useGravity in PlayerRigidbody ? i can't show in my script. \$\endgroup\$ – Darshan Soni Jul 23 '16 at 9:56
  • \$\begingroup\$ I do that both way as u say..but it's doesn't effect. player can freeze when die. can not goes down by gravity. when the disable tilting can not change player gravity or isKinematic value. don't know why but it's happen. \$\endgroup\$ – Darshan Soni Jul 23 '16 at 11:48
  • \$\begingroup\$ About useGravity I just re-used your example. I thought it was an accessor to player RigidBody component. useGravity is a field of RigidBody : docs.unity3d.com/ScriptReference/Rigidbody-useGravity.html \$\endgroup\$ – lvictorino Jul 23 '16 at 16:05
0
\$\begingroup\$

I Solve the Problem using attached Player rigidbody2d to the Inspector. Create object of Rigidbody2D Public Rigidbody2d PlayerRigidBody; and Attach Player to the PlayerRigidBody in Inspector then Solve The problem. Thank to all who give me any suggestion.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.