Hi! I'm using a copy of my Player GameObject but when it's active I can't controll my player now.
Here is a small part of my code!
With the script bellow if the isEnemy bool is set to true, my enemy moves to my position but I can't move my player at all, not the enemy (If I set isEnemy to false to my enemy i can move the enemy but not my main charater)
public class _PlayerController : MonoBehaviour
{
public bool isEnemy = false;
public bool playerCanJump = true;
public bool playerGrounded = true;
public bool playerCanAttack = true;
public int activeHand = 1; // 0 = left; 1 = right;
public GameObject head;
public GameObject leftArm;
public GameObject leftLeg;
public GameObject leftWeapon;
public GameObject rightArm;
public GameObject rightLeg;
public GameObject rightWeapon;
public GameObject torso;
// Private
private Rigidbody2D _headRb;
private Rigidbody2D _leftArmFistRb;
private Rigidbody2D _leftArmRb;
private Rigidbody2D _leftLegRb;
private Rigidbody2D _leftWeaponRb;
private Rigidbody2D _rightArmRb;
private Rigidbody2D _rightLegRb;
private Rigidbody2D _rightWeaponRb;
private Rigidbody2D _torsoRb;
private Vector2 _jumpVector;
private Vector2 _moveVector;
private Vector2 _attackRightVector;
private Vector2 _attackRightTorsoVector;
private Vector2 _attackLeftVector;
private Vector2 _attackLeftTorsoVector;
private GameObject playerTag;
private void Start()
{
_headRb = head.GetComponent();
_torsoRb = torso.GetComponent();
_leftArmRb = leftArm.GetComponent();
_rightArmRb = rightArm.GetComponent();
_leftLegRb = leftArm.GetComponent();
_rightLegRb = rightLeg.GetComponent();
_rightWeaponRb = rightWeapon.GetComponent();
_leftWeaponRb = leftWeapon.GetComponent();
_jumpVector = new Vector2(0.0f, 2.0f);
_attackRightVector = new Vector2(1.0f, 1.0f);
_attackRightTorsoVector = new Vector2(-1.0f, -1.0f);
_attackLeftVector = new Vector2(-1.0f, 1.0f);
_attackLeftTorsoVector = new Vector2(1.0f, 1.0f);
playerTag = GameObject.FindGameObjectWithTag("Player");
SetActiveHand(GetActiveHand());
}
private void Update()
{
if (!isEnemy)
{
if (Input.GetKeyDown(KeyCode.Space) && GetPlayerCanJump())
{
Jump();
}
if (Input.GetKey(KeyCode.D))
{
Move(-2.0f);
}
if (Input.GetKey(KeyCode.A))
{
Move(2.0f);
}
}
else
{
// Move enemy to me using the same Move() function.
}
}
public void Jump()
{
_headRb.AddForce(_jumpVector * 20, ForceMode2D.Impulse);
SetPlayerCanJump(false);
SetPlayerGrounded(false);
}
public void Move(float x)
{
if (GetPlayerOnGround())
{
_moveVector = new Vector2(-x, 0.5f);
_headRb.AddForce(_moveVector * 5, ForceMode2D.Impulse);
SetPlayerGrounded(false);
}
}
}
↧