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 playercontroller : MonoBehaviour
- {
- public GameObject hitbox;
- private Rigidbody rb;
- private Vector3 totalVelocity;
- public float speed = 100;
- // Start is called before the first frame update
- void Start()
- {
- rb = GetComponent<Rigidbody>();
- rb.freezeRotation = true;
- Vector2 playerDirectionalInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- //setting velocity to zero, then calculating velocity depending on arrow keys
- //sum of velocity is then set to rigidbody
- totalVelocity = Vector3.zero;
- if (Input.GetKey(KeyCode.RightArrow))
- {
- totalVelocity += transform.right * speed * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.LeftArrow))
- {
- totalVelocity -= transform.right * speed * Time.deltaTime;
- }
- if(Input.GetKey(KeyCode.UpArrow))
- {
- totalVelocity += transform.forward * speed * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.DownArrow))
- {
- totalVelocity -= transform.forward * speed * Time.deltaTime;
- }
- if (totalVelocity != Vector3.zero)
- {
- //rotate velocity to be relative to camera
- totalVelocity = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0) * totalVelocity;
- //rotate character to be relative to velocity
- transform.rotation = Quaternion.LookRotation(totalVelocity, Vector3.up);
- }
- //setting rb velocity
- rb.velocity = totalVelocity;
- //set hitbox active depending on spacebar
- if(Input.GetKey(KeyCode.Space))
- {
- hitbox.SetActive(true);
- }
- else
- {
- hitbox.SetActive(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement