all repos — RPG @ 03e9f8a85e59bff34f1971951395812f0bac367f

Fully functional 3D turn based role playing game coded in C# and base Unity.

Assets/Scripts/Overworld/NoJumpController.cs (view raw)

 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4
 5public class NoJumpController : MonoBehaviour
 6{
 7    public float moveSpeed;
 8    public CharacterController controller;
 9    public float gravityScale;
10    public Transform cameraTransform;
11    private Animator anim;
12    private Vector3 moveHorizontal;
13    private Vector3 moveVertical;
14    
15
16    //private bool first = true;
17
18    // Start is called before the first frame update
19    void Start()
20    {
21        controller = GetComponent<CharacterController>();
22        anim = GetComponentInChildren<Animator>();
23    }
24
25    // Update is called once per frame
26    void Update()
27    {
28        
29        moveHorizontal = (cameraTransform.forward * Input.GetAxis("Vertical")) + (cameraTransform.right * Input.GetAxis("Horizontal"));
30        moveHorizontal = moveHorizontal.normalized * moveSpeed;
31        moveHorizontal.y = 0;
32        if (controller.isGrounded)
33        {
34            moveVertical.y = 0f;
35        } else
36        {
37            moveVertical.y = moveVertical.y + (Physics.gravity.y * gravityScale);
38        }
39        
40
41        
42
43        controller.Move((moveHorizontal + moveVertical)* Time.deltaTime);
44
45        if (moveHorizontal != Vector3.zero)
46        {
47            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(new Vector3(moveHorizontal.x, 1, moveHorizontal.z)), 0.15F);
48            anim.SetBool("Running", true);
49        } else
50        {
51            anim.SetBool("Running", false);
52        }
53            
54    }
55}