Search Unity

tutorial help

Discussion in 'Physics' started by spirachi77, Dec 2, 2017.

  1. spirachi77

    spirachi77

    Joined:
    Sep 26, 2017
    Posts:
    17
    I am following this tutorial but everytime I press space (my attack button) it returns that it hit my attack box and only works when the attack box is on my hitbox layermask

    my code:

    public class playerController : MonoBehaviour {
    [SerializeField]
    float movespd=4f;
    Vector3 forward, right;
    public Collider[] hitBox;
    // 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 (Input.GetKeyDown(KeyCode.Space))
    {
    WeaponAttack(hitBox[0]);
    }
    if (Input.anyKey) {
    Move();
    }


    }
    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;
    }
    void WeaponAttack(Collider col) {
    Collider[] cols = Physics.OverlapBox(col.bounds.center, col.bounds.extents, col.transform.rotation, LayerMask.GetMask("hitbox"));
    foreach (Collider c in cols)
    Debug.Log(c.name);
    }
    }

    thanks in advance
     
    Last edited: Dec 3, 2017