VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_ThirdPersonCharacterController.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 using AVR.Core;
6 
7 namespace AVR.Net {
8  /// <summary>
9  /// Simple 3rd person character controller script.
10  /// </summary>
11  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_net_1_1_a_v_r___third_person_character_controller.html")]
13  {
14  public Animator anim;
15  public CharacterController cc;
16 
17  public AnimationCurve accel_curve;
18  public AnimationCurve dccel_curve;
19  public float turnspeed = 100.0f;
20  public float max_speed = 3.0f;
21  public float min_speed = -3.0f;
22  public float jump_force = 5.0f;
23 
24  private float speed = 0.0f;
25  private float yspeed = 0.0f;
26  private float accel_t = 0.0f;
27  private float dccel_t = 0.0f;
28 
29  // Start is called before the first frame update
30  protected override void Start()
31  {
32  anim = GetComponent<Animator>();
33  cc = GetComponent<CharacterController>();
34  }
35 
36  // Update is called once per frame
37  void Update()
38  {
39  Vector3 movement = Vector3.zero;
40 
41  if(Input.GetKey(KeyCode.Mouse1)) {
42  transform.Rotate(Vector3.up, Input.GetAxis("Mouse X"), Space.Self);
43  //transform.Rotate(Vector3.left, Input.GetAxis("Mouse Y"), Space.Self);
44  Camera.main.transform.RotateAround(transform.position, Camera.main.transform.right, -Input.GetAxis("Mouse Y"));
45  }
46 
47  if (cc.isGrounded)
48  {
49  // Forward / Backward
50  if (Input.GetKey(KeyCode.W))
51  {
52  //speed = Mathf.Clamp(speed + accel * Time.deltaTime, 0.0f, max_speed);
53  speed = max_speed * accel_curve.Evaluate(accel_t);
54  accel_t += Time.deltaTime;
55  dccel_t = 0.0f;
56  }
57  else if (Input.GetKey(KeyCode.S))
58  {
59  speed = min_speed * accel_curve.Evaluate(accel_t);
60  accel_t += Time.deltaTime;
61  dccel_t = 0.0f;
62  }
63  else
64  {
65  //speed = Mathf.Clamp(speed - accel * Time.deltaTime, 0.0f, max_speed);
66  speed = max_speed * dccel_curve.Evaluate(dccel_t);
67  dccel_t += Time.deltaTime;
68  accel_t = 0.0f;
69  }
70 
71  // Turning
72  if (Input.GetKey(KeyCode.A))
73  {
74  transform.Rotate(new Vector3(0, -turnspeed * Time.deltaTime, 0), Space.Self);
75  }
76  if (Input.GetKey(KeyCode.D))
77  {
78  transform.Rotate(new Vector3(0, turnspeed * Time.deltaTime, 0), Space.Self);
79  }
80 
81  // Jumping
82  if (Input.GetKeyDown(KeyCode.Space) && cc.isGrounded)
83  {
84  yspeed = jump_force;
85  anim.SetBool("Jump", true);
86  }
87  else if (cc.isGrounded && yspeed > 0)
88  {
89  yspeed = 0.0f;
90  anim.SetBool("Jump", false);
91  }
92  }
93 
94 
95  movement = transform.forward * speed;
96  movement.y = yspeed;
97 
98  cc.Move(movement * Time.deltaTime);
99 
100  anim.SetFloat("Speed", speed);
101  }
102  }
103 }
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
Simple 3rd person character controller script.
Base class for all arc-vr components. A component is typically a Monobehaviour that represents a virt...