View difference between Paste ID: sNMRdSy7 and 2pT1f0iF
SHOW: | | - or go back to the newest paste.
1
using System.Collections;
2
using System.Collections.Generic;
3
using UnityEngine;
4
5
public class player : MonoBehaviour
6
{
7
    #region variables_and_start
8
    private bool wPressed;
9
    private bool aPressed;
10
    private bool sPressed;
11
    private bool dPressed;
12
    private Rigidbody rb;
13
    private bool isGrounded;
14
15
    public float speed;
16
    public float jumpForce;
17
    public GameObject Camera;
18
    // Start is called before the first frame update
19
    void Start()
20
    {
21
        rb=GetComponent<Rigidbody>();
22
    }
23
    #endregion
24
#region Moving
25
    // Update is called once per frame
26
    void Update()
27
    {
28
        Ray ray = new Ray(transform.position,-transform.up);
29
        RaycastHit hit;
30
        if(Physics.Raycast(ray,out hit)){
31
            if(hit.distance<=1.1f){
32
                isGrounded=true;
33
            }
34
            else{
35
                isGrounded=false;
36
            }
37
        }
38
        if(Input.GetKeyDown(KeyCode.W)){
39
            wPressed=true;
40
        }
41
        if(Input.GetKeyUp(KeyCode.W)){
42
            wPressed=false;
43
        }
44
        if(Input.GetKeyDown(KeyCode.A)){
45
            aPressed=true;
46
        }
47
        if(Input.GetKeyUp(KeyCode.A)){
48
            aPressed=false;
49
        }
50
        if(Input.GetKeyDown(KeyCode.S)){
51
            sPressed=true;
52
        }
53
        if(Input.GetKeyUp(KeyCode.S)){
54
            sPressed=false;
55
        }
56
        if(Input.GetKeyDown(KeyCode.D)){
57
            dPressed=true;
58
        }
59
        if(Input.GetKeyUp(KeyCode.D)){
60
            dPressed=false;
61
        }
62
        if(Input.GetKeyDown(KeyCode.Space)&&isGrounded){
63
            rb.AddForce(transform.up*jumpForce,ForceMode.Impulse);
64
        }
65
        Ray shoot = new Ray(Camera.transform.position,transform.forward*Mathf.Infinity);
66
        Debug.DrawRay(Camera.transform.position,transform.forward*Mathf.Infinity,Color.blue,100f);
67
68
        if(Input.GetMouseButtonDown(0)){
69
            RaycastHit hit1;
70
            if(Physics.Raycast(shoot,out hit1)){
71
                Debug.Log(hit.collider.gameObject.name);
72
                if(hit1.collider.gameObject.tag=="Zombi"){
73
                    Debug.Log("Enemy");
74
                    Zombi z = hit.collider.gameObject.GetComponent<Zombi>();
75
                    z.health-=50;
76
                }
77
            }
78
        }
79
    }
80
    void FixedUpdate()
81
    {
82
        if(wPressed){
83
            transform.Translate(0,0,speed*0.01f);
84
        }
85
        if(aPressed){
86
            transform.Translate(-speed*0.01f,0,0);
87
        }
88
        if(sPressed){
89
            transform.Translate(0,0,-speed*0.01f);
90
        }
91
        if(dPressed){
92
            transform.Translate(speed*0.01f,0,0);
93
        }
94
        #endregion
95
    }
96
}
97