Unity Experiments: FPC Coded Scripts 03:02

Starting from where I left off in the Wheels project, I have researched how to create a First Person Controller with code and also using a character controller.

First Person Controller Code 
(From Tutorial) 


Controller Body Script

Using System.collections; 
Using System.collections.Generic; 
Using UnityEngine; 

Public class PlayerMovementController:MonoBehaviour 
{
[SerializeField] private float speed;
[SerializeField] private float jumpForce;
[SerializeField] private float raycastDistance;
private Rigidbody rb;

private void start()
{
rb = GetComponent<Rigidbody>();
}

private void Update ()
{
Jump();
}

private void FixedUpdate()
}

Move()
}

private void Move()
{

float hAxis = Input.GetAxisRaw(“Horizontal”);
float hAxis = Input.GetAxisRaw(“Vertical”);

Vector3 movement = new Vector3 (hAxis,0,vAxis) * speed;

Vector3 newPosition = rb.position + rb.transform.TransformDirection(movement);

rb.MovePosition (newPosition);
}

private void Jump()
{

if (Input.GetKeyDown(KeyCode.Space))
{

if (Isgrounded()
{

rb.AddForce(0,JumpForce,0,ForceMode.Impulse);
}
}

private bool IsGrounded()
{
Debug.DrawRay(transform.position,Vector3.down * raycastDistance, Color.blue);

return(Physics.Raycast(transform.position,Vector3.down.raycastDistance)
}




Player Camera Controller Script

Using System.collections; 
Using System.collections.Generic; 
Using UnityEngine; 

public class PlayerCameraController:MonoBehaviour
{
[SerializeField] private float lookSensitivity;
[SerializeField] private float Smoothing;

private GameObject player;
private Vector2 smoothedVelocity;
private Vector2 currentLookingPos;
void Start ()
}

player = transform.parent.gameObject;
Cursor.LockState = CursorLockMode.Locked;
Cursor.visible = false;

private Void Update()
{
RotateCamera();
CheckForShooting();
}

private void RotateCamera()
{
Vector2 inputValues = new Vector2(Input.GetAxisRaw(“Mouse x”), (this is all on one line) Input.GetAxisRaw(“Mouse Y”));

inputValues = Vector2.scale(inputValues,new Vector2 (lookSensitivity * smoothing,lookSensitivity * smoothing )); 

smoothedVelocity.x = maths.Lerp (smoothedVelocity.y,inputValues.y, 1f / smoothing);

currentLookingPos += smoothedVelocity;

transform.localRotation = Quatenum.AngleAxis(-currentLookingPos.Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis (currentLookingPos.x, player.transform.up);
}

private CheckForShooting()
}
if (Input.GetMouseButtonDown (0))
{
RaycastHit whatIHit;
if (Physics.Raycast(transform.position,transform.forward,out whatIHit, Mathf.Infinity))
{
Debug.Log (whatIHit.collider.name);
}
}
}
}







Unity Experiments: FPC Coded Scripts 03:02

Starting from where I left off in the Wheels project, I have researched how to create a First Person Controller with code and also using a character controller.

First Person Controller Code 
(From Tutorial) 


Controller Body Script

Using System.collections; 
Using System.collections.Generic; 
Using UnityEngine; 

Public class PlayerMovementController:MonoBehaviour 
{
[SerializeField] private float speed;
[SerializeField] private float jumpForce;
[SerializeField] private float raycastDistance;
private Rigidbody rb;

private void start()
{
rb = GetComponent<Rigidbody>();
}

private void Update ()
{
Jump();
}

private void FixedUpdate()
}

Move()
}

private void Move()
{

float hAxis = Input.GetAxisRaw(“Horizontal”);
float hAxis = Input.GetAxisRaw(“Vertical”);

Vector3 movement = new Vector3 (hAxis,0,vAxis) * speed;

Vector3 newPosition = rb.position + rb.transform.TransformDirection(movement);

rb.MovePosition (newPosition);
}

private void Jump()
{

if (Input.GetKeyDown(KeyCode.Space))
{

if (Isgrounded()
{

rb.AddForce(0,JumpForce,0,ForceMode.Impulse);
}
}

private bool IsGrounded()
{
Debug.DrawRay(transform.position,Vector3.down * raycastDistance, Color.blue);

return(Physics.Raycast(transform.position,Vector3.down.raycastDistance)
}




Player Camera Controller Script

Using System.collections; 
Using System.collections.Generic; 
Using UnityEngine; 

public class PlayerCameraController:MonoBehaviour
{
[SerializeField] private float lookSensitivity;
[SerializeField] private float Smoothing;

private GameObject player;
private Vector2 smoothedVelocity;
private Vector2 currentLookingPos;
void Start ()
}

player = transform.parent.gameObject;
Cursor.LockState = CursorLockMode.Locked;
Cursor.visible = false;

private Void Update()
{
RotateCamera();
CheckForShooting();
}

private void RotateCamera()
{
Vector2 inputValues = new Vector2(Input.GetAxisRaw(“Mouse x”), (this is all on one line) Input.GetAxisRaw(“Mouse Y”));

inputValues = Vector2.scale(inputValues,new Vector2 (lookSensitivity * smoothing,lookSensitivity * smoothing )); 

smoothedVelocity.x = maths.Lerp (smoothedVelocity.y,inputValues.y, 1f / smoothing);

currentLookingPos += smoothedVelocity;

transform.localRotation = Quatenum.AngleAxis(-currentLookingPos.Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis (currentLookingPos.x, player.transform.up);
}

private CheckForShooting()
}
if (Input.GetMouseButtonDown (0))
{
RaycastHit whatIHit;
if (Physics.Raycast(transform.position,transform.forward,out whatIHit, Mathf.Infinity))
{
Debug.Log (whatIHit.collider.name);
}
}
}
}