So I am making a game based on, when you touch a wall you get shot away from that wall into the next one, just making you go back and forth so fast that you explode. The problem is that I don’t know how to execute this.
I am trying to simply make the player bounce off an collider attached to the map terrain.
I am trying to execute it with this:
using UnityEngine;
using System.Collections;
public class bounceArea : MonoBehaviour {
public int mapForce = 200;
void OnTriggerEnter2D(Collider2D col){
Vector3 dir = col.transform.position - transform.position; dir.y = 0;
if (col.CompareTag("BounceArea")) {
col.attachedRigidbody.AddForce(dir.normalized * mapForce);
}
}
}
But I cannot make it work, I would really like the code to be attached to the collider on the map instead of the player, but with my code that wont happen. (used a similar script for my melee)
I’ve done something similar, instead of using AddForce I just changed the velocity directly for a period of time to create a “knockback” effect (e.g. while being knocked back, I have this velocity and no other velocity from player input).
You might want to do it like this (using Vector2 but the same principle applies):
Vector2 knockbackVelocity =
new Vector2((transform.position.x - player.transform.position.x) * forceMultiplier, (transform.position.y - player.transform.position.y) * forceMultiplier);
player.GetComponent<Rigidbody2D>().velocity = -knockbackVelocity;
Basically, what this means is: Knock the player back directly away from the source of where they collided with “me” (whatever causes the knockback, e.g. a wall). The
* forceMultiplier
Is just a way of increasing or decreasing the “force”, so 4f might not be a lot, but 100f would be loads (so if you wanted to keep increasing the speed with each bounce you could just increment this value via a counter or something). Lastly, negative knockbackVelocity is applied because we want to move away from the source of knockback.
This is just one way of doing it, you can use AddForce or whatever, and for Vector3 you’ll just need to include the extra fields that aren’t needed for a Vector2 and do more or less the same calculation.
EDIT: In response to your last comment, if you’ve got the “bounceArea” applied to the entire terrain, then my method won’t work very well as it will try and knock the player away from the entire terrain’s “position” (i.e. one set of xyz coords corresponding to the terrain gameobject itself), which could be anywhere (and will mean the player always gets bounced away from there regardless of where they actually are). One way of doing it would be to create multiple smaller terrains to bounce away from, but a better way would probably be to somehow work out where the player hit the terrain, and apply a similar velocity calculation away from that point instead of the terrain object’s xyz.
Did you consider simply inversing the velocity and multiplying it? rigidbody2D.velocity *= -2.0f is what I mean. Alternatively squaring it is also an option but you would reach extreme values rather quickly.