Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class player : MonoBehaviour
- {
- #region variables_and_start
- private bool wPressed;
- private bool aPressed;
- private bool sPressed;
- private bool dPressed;
- private Rigidbody rb;
- private bool isGrounded;
- public float speed;
- public float jumpForce;
- public GameObject Camera;
- // Start is called before the first frame update
- void Start()
- {
- rb=GetComponent<Rigidbody>();
- }
- #endregion
- #region Moving
- // Update is called once per frame
- void Update()
- {
- Ray ray = new Ray(transform.position,-transform.up);
- RaycastHit hit;
- if(Physics.Raycast(ray,out hit)){
- if(hit.distance<=1.1f){
- isGrounded=true;
- }
- else{
- isGrounded=false;
- }
- }
- if(Input.GetKeyDown(KeyCode.W)){
- wPressed=true;
- }
- if(Input.GetKeyUp(KeyCode.W)){
- wPressed=false;
- }
- if(Input.GetKeyDown(KeyCode.A)){
- aPressed=true;
- }
- if(Input.GetKeyUp(KeyCode.A)){
- aPressed=false;
- }
- if(Input.GetKeyDown(KeyCode.S)){
- sPressed=true;
- }
- if(Input.GetKeyUp(KeyCode.S)){
- sPressed=false;
- }
- if(Input.GetKeyDown(KeyCode.D)){
- dPressed=true;
- }
- if(Input.GetKeyUp(KeyCode.D)){
- dPressed=false;
- }
- if(Input.GetKeyDown(KeyCode.Space)&&isGrounded){
- rb.AddForce(transform.up*jumpForce,ForceMode.Impulse);
- }
- Ray shoot = new Ray(Camera.transform.position,transform.forward*Mathf.Infinity);
- Debug.DrawRay(Camera.transform.position,transform.forward*Mathf.Infinity,Color.blue,100f);
- if(Input.GetMouseButtonDown(0)){
- RaycastHit hit1;
- if(Physics.Raycast(shoot,out hit1)){
- Debug.Log(hit.collider.gameObject.name);
- if(hit1.collider.gameObject.tag=="Zombi"){
- Debug.Log("Enemy");
- Zombi z = hit.collider.gameObject.GetComponent<Zombi>();
- z.health-=50;
- }
- }
- }
- }
- void FixedUpdate()
- {
- if(wPressed){
- transform.Translate(0,0,speed*0.01f);
- }
- if(aPressed){
- transform.Translate(-speed*0.01f,0,0);
- }
- if(sPressed){
- transform.Translate(0,0,-speed*0.01f);
- }
- if(dPressed){
- transform.Translate(speed*0.01f,0,0);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement