- Home /
Fire Knockback
I can imagine this:
I just cannot code it in C#. I think the script should be on fire prefab and it should detect the charactercontroller collision and get the point firstly. Then send charactercontroller by some amount in the opposite direction with gravity so it stops somewhere. (Oh and it should be collision point(x,charactercollider's bottom.y,z) if collision is a line. I don't know how it would detect the collision point in ths situation honestly.)
Am I on the right track? Is the script should be attached to fire or character? How to create the direction and move charactercontroller in that way?
Sorry, I'm a beginner but this will help me understand a lot of things. Can anyone help me out?
Answer by YoungDeveloper · Jan 23, 2014 at 01:15 AM
for detailed question explanation, instead of "plz help need script".
Anyway, you are on right track. I suggest checking the collison only on player, because if you will have 5 fires, there will be 5 collision checks each frame.
When player collides or triggers, get direction vector from fire to player(you know fire GO from collision), then call some method which apply force and to player in that vector* strength from fire, and create some method to remove heath.
public float health = 100f;
void RemoveHealth(){
health -= 5f;
}
//When you want to remove health, just call the method
RemoveHealth();
Direction: http://docs.unity3d.com/Documentation/Manual/DirectionDistanceFromOneObjectToAnother.html
Force: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
Thank you very much.
I think Force can only be used if character has a rigidbody. In this case I have character controller. I guess I'll just start to code by checking collision and producing the vector in the right direction. I could manage with damage code but still thanks :)
Answer by Alpha_Guac · Oct 19, 2015 at 10:40 AM
have the collision attached to the player. and try
void OnCollisionEnter(Collider c) if(c.gameObject == "Fire") { Health-= fireDamage; this.rigidbody.addForce(force applied in knockback as Vector3, ForceMode that you are using) }
check this out to determine what force mode you want to use. http://docs.unity3d.com/ScriptReference/ForceMode.html
Answer by RavenOfCode · Jan 04, 2016 at 01:23 PM
Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void TakeKnockback(float amount, GameObject hitter)
{
rb.AddForce((transform.position - hitter.transform.position) * 100 * amount);
}
Call TakeKnockback on what is getting hit, use hitter as the object who hit.
Your answer
Follow this Question
Related Questions
Can't change vector direction 0 Answers
Vector direction after collision 1 Answer
Slerp look at direction not working... 3 Answers