- Home /
How I can do knockback without OnColisson or OnTrigger
I want my player to have knockback when my enemy hits him but I do not want to use OnTrigger or OnCollision ,I want my player to have knockback when my enemy hits him but I do not want to use OnTrigger or OnCollision
This is script I use, my knockback work but have some bugs the I can't fix
Answer by I5 · Sep 13, 2018 at 02:04 AM
did you forget to include the script? It's not in your post.
If I understand, you do NOT want to use OnTrigger or OnCollision.
Another option would be use a raycast from the center/core part of whatever enemy collider will be making contact with the player. You'd have to do this in Update, so a raycast every frame and see if and where the raycast hit the player. You'd just raycast from the enemy to the player and if the hit distance was, for example 0.1 (or some distance to indicate they're so close that they've collided). However you won't be able to get fined-grained collision info (like an enemy hand hitting a player's head) unless you use OnColisionEnter.
Answer by guilhermeprata · Sep 13, 2018 at 05:38 PM
Só I try with add force but no worked so well this is my script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class HandleMovement : MonoBehaviour {
Rigidbody2D rb;
StateManager states;
HandleAnimations anim;
public float acceleration = 30;
public float airAcceleration = 15;
public float maxSpeed = 60;
public float jumpSpeed = 5;
public float jumpDuration = 5;
float actualSpeed;
bool justJumped;
bool canVariableJump;
float jmpTimer;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
states = GetComponent<StateManager>();
anim = GetComponent<HandleAnimations>();
rb.freezeRotation = true;
}
void FixedUpdate ()
{
if(!states.dontMove)
{
HorizontalMovement();
Jump();
}
if (states.gettingHit && states.onGround && justJumped == false && states.canAttack)
{
if (states.lookRight)
{
rb.AddRelativeForce(Vector3.left * 13);
states.dontMove = true;
}
else
{
rb.AddRelativeForce(Vector3.right * 13);
}
}
}
void HorizontalMovement()
{
actualSpeed = this.maxSpeed;
if(states.onGround && !states.currentylAttacking)
{
rb.AddForce(new Vector2((states.horizontal * actualSpeed) - rb.velocity.x * this.acceleration,0));
}
//caso esta sliding
if(states.horizontal == 0 && states.onGround)
{
rb.velocity = new Vector2(0, rb.velocity.y);
}
}
void Jump()
{
if(states.vertical >0)
{
if(!justJumped)
{
justJumped = true;
if(states.onGround)
{
anim.JumpAnim();
rb.velocity = new Vector3(rb.velocity.x, this.jumpSpeed);
jmpTimer = 0;
canVariableJump = true;
}
}
else
{
if(canVariableJump)
{
jmpTimer += Time.deltaTime;
if(jmpTimer < this.jumpDuration / 1000)
{
rb.velocity = new Vector3(rb.velocity.x, this.jumpSpeed);
}
}
}
}
else
{
justJumped = false;
}
}
public void AddVelocityOnCharacter(Vector3 direction, float timer)
{
StartCoroutine(AddVelocity(timer, direction));
}
IEnumerator AddVelocity(float timer,Vector3 direction)
{
float t = 0;
while(t<timer)
{
t += Time.deltaTime;
rb.AddForce(direction * 2);
yield return null;
}
}
}
Your answer
Follow this Question
Related Questions
How am I able to move my (Player) character in the opposite direction of where I am shooting? 3 Answers
Adding force to mouse position adds force relative to screen center 0 Answers
Projectiles always going to the left and not forward 2 Answers
AddForce knockback x axis according to mouse.position more then y axis (2D game) 0 Answers
Rigidbody2d.AddForce behaving odd 1 Answer