Search Unity

knockback script help

Discussion in 'Scripting' started by spirachi77, Jan 22, 2018.

  1. spirachi77

    spirachi77

    Joined:
    Sep 26, 2017
    Posts:
    17
    I want to make an isometric rpg and need help with my knock back script. My character just teleports somewhere for a second and then is able to move.

    player script:

    [SerializeField]
    public float movespd = 4f;
    Vector3 forward, right;
    public Collider[] hitBox;
    public float damage;
    public float knockbackForce;
    public float knockbackTime;
    private float knockbackCounter;
    // Use this for initialization
    void Start()
    {
    forward = Camera.main.transform.forward;
    forward.y = 0;
    forward = Vector3.Normalize(forward);
    //changes where the vector is facing
    right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
    }
    // Update is called once per frame
    void Update()
    {
    if (knockbackCounter <= 0)
    {
    if (Input.GetKeyDown(KeyCode.Space))
    {
    WeaponAttack(hitBox[0]);
    }
    if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
    {
    Move();
    }
    }
    else
    {
    knockbackCounter -= Time.deltaTime;
    }
    }
    void Move()
    {
    Vector3 direction = new Vector3(Input.GetAxis("horizontal key"), 0f, Input.GetAxis("vertical key"));
    Vector3 rightMovement = right * movespd * Time.deltaTime * Input.GetAxis("horizontal key");
    Vector3 upMovement = forward * movespd * Time.deltaTime * Input.GetAxis("vertical key");
    Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
    transform.forward = heading;
    transform.position += rightMovement;
    transform.position += upMovement;
    }
    public void Knockback(Vector3 kncPos)
    {
    knockbackCounter = knockbackTime;
    kncPos = new Vector3(1f, 1f, 1f);
    transform.position += kncPos * knockbackForce * Time.deltaTime;
    }
    void WeaponAttack(Collider col)
    {
    string oTag = col.tag;
    Collider[] cols = Physics.OverlapBox(col.bounds.center, col.bounds.extents, col.transform.rotation, LayerMask.GetMask("hitbox"));
    col.tag = "Hit Box";
    foreach (Collider c in cols)
    {
    Debug.Log(c.name);
    switch (c.tag)
    {
    case "Hurt Box":
    c.SendMessageUpwards("TakeDamage", damage);
    break;
    default:
    Debug.Log("null");
    break;
    }
    }
    col.tag = oTag;
    }

    other hitbox script:

    public float hurtValue;
    public PlayerController p1;
    // Use this for initialization
    void Start () {
    p1 = FindObjectOfType<PlayerController>();
    }

    // Update is called once per frame
    void Update () {

    }
    private void OnTriggerEnter(Collider other) {
    if (other.gameObject.tag == "Player") {
    Debug.Log("hit");
    Vector3 hitDirection = other.transform.position - transform.position;
    hitDirection = hitDirection.normalized;
    FindObjectOfType<PlayerController>().Knockback(hitDirection);
    }
    }
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667