Question by
Creative Inventory · Nov 16, 2015 at 07:14 PM ·
not workingbounce
Bouncy on player not working
When my player collides with an tagged object my player isn't being pushed forwards, it just constantly freezes the positions (X &Y) and the rotation in the Rigidbody (which I already know). Do I need to add or increase the drag in the Rigidbody or change my tags or something that I'm missing? Can someone help?
public class PlayerController : MonoBehaviour {
public float force = 10f;
float speed = 5f;
float freezeTime = 1f;
bool isCollide = false;
void FixedUpdate ()
{
float dirX = Input.GetAxis ("Horizontal");
float dirY = Input.GetAxis ("Vertical");
Vector3 movement = new Vector2 (dirX, dirY);
if(isCollide==false)
GetComponent<Rigidbody2D> ().velocity = movement * speed;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Boundary") {
isCollide = true;
GetComponent<Rigidbody2D> ().AddForce (-GetComponent<Rigidbody2D> ().velocity * force, ForceMode2D.Impulse);
Invoke("StartFreeze", 0.1f);
}
}
void StartFreeze()
{
GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.FreezeAll;
Invoke("ExitFreeze", freezeTime);
}
void ExitFreeze()
{
GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.None;
isCollide = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
Player bounces off of enemy. Help!! 1 Answer
in Google Play application does not open at all! What happened? How to fix? What is the reason??? 0 Answers
WebGL 2.0 not working on linux on latest Chrome with WebGL2: Hardware accelerated 0 Answers
Limit bouncing to x and y axis only, not z axis. 0 Answers
How do I make a ball have force where it bounces and adds force in the direction it is going at? 0 Answers