Hi,
I want to change my Player’s BoxCollider2D size by scripting when I do a specifical action.
I need your help ^^’ please.
this is my script, the only : (attach to my player)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public Vector2 speed = new Vector2(10,10);
private Vector2 movement;
[HideInInspector]
public bool facingRight = true; // For determining which way the player is currently facing.
[HideInInspector]
public bool jump = false;
public float moveForce = 365f; // Amount of force added to move the player left and right.
public float maxSpeed = 5f; // The fastest the player can travel in the x axis.
public float jumpForce = 1000;
private Animator anim;
private bool grounded = false;
public bool mirrorActivator = false;
BoxCollider2D b = gameObject.GetComponent<BoxCollider2D>(); // tempt to inisialized my BoxCollider2D
void Start()
{
anim = GetComponent<Animator>();
anim.SetBool("isNormal", true);
}
void Update ()
{
anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
// retrieve axis information
float inputX = Input.GetAxis("Horizontal");
movement = new Vector2( speed.x * inputX,0);
if (Input.GetButtonDown("Jump") grounded == true)
{
jump = true;
grounded = false;
}
if (Input.GetButtonDown("Up"))
{
mirrorActivator = true;
}
else if (Input.GetButtonUp("Up"))
{
mirrorActivator = false;
}
Debug.Log (mirrorActivator);
}
void FixedUpdate ()
{
//Move player
rigidbody2D.velocity = movement;
if (jump)
{
rigidbody2D.velocity = new Vector2 (0, jumpForce * Time.deltaTime);
//rigidbody2D.AddForce(Vector2.up * jumpForce);
jump = false;
}
float h = Input.GetAxis("Horizontal");
if(h * rigidbody2D.velocity.x < maxSpeed)
{
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
}
// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
{
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
}
// If the input is moving the player right and the player is facing left...
if(h > 0 !facingRight)
{
// ... flip the player.
Flip();
}
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 facingRight)
{ // ... flip the player.
Flip();
}
}
void Flip ()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.name == "Platform")
{
grounded = true;
}
}
void OnTriggerEnter2D (Collider2D trig)
{
if (trig.gameObject.name == "MirrorSlim")
{
if ( mirrorActivator == true )
{
anim.SetBool("isSlim", true);
anim.SetBool("isNormal", false);
b.size = Vector2 ( 1.0f, 3.0f); // tempt to change my BoxCollider2D size.
}
}
}
}
1509454–85557–$PlayerController.cs (2.9 KB)