Hello fellow developers. So, the last week I’ve been fighting to get a camera that faces the same direction as the character to work. My solution was to use a plane, slap a crosshair texture on it, dub that as a ‘Follower’ object in the Hierarchy which the camera always faces, and stick the camera a certain distance from the player. I’m focusing on the main controls of the game, so my next concern is movement. Since I haven’t worked on any animations yet, it’s all transformations. I can make the character rotate correctly, strafe and walk forward, but there is one problem: the character (and that is to be expected) doesn’t move in the local forward direction (that is, towards the crosshair) but along the global Z axis. What I want to achieve is, when the player presses the Forward button, the character moves towards the crosshair as in most modern TPS shooters. I’ll give you my code below.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
/* This script controls all aspects of player behavior. It includes moveDirectional, shooting and various other interactions */
public float playerSpeed = 4f; // Player's moveDirectional speed
public float jumpSpeed = 400f; // Player's jump speed
public float jumpMaxDistance = 0.5f; // Public for debugging purposes
public float sensitivity = 1f;
public float rotSmooth = 1f;
private Vector3 playerPos;
Vector3 newMouseMove;
Vector3 moveDirectional; // Motion vector
Vector3 moveMouse;
Rigidbody player_rb; // Reference to the player's rigidbody
Transform player_trans; // Reference to the player's transform
GameObject follower; // Reference to the follower crosshair
void Awake()
{
/* ----- SETUP ----- */
follower = GameObject.FindGameObjectWithTag("Follower");
player_rb = GetComponent<Rigidbody>();
player_trans = GetComponent<Transform>();
}
void Update()
{
/* DEBUGGING */
Debug.DrawLine(player_trans.position, new Vector3(0f,0f,0f));
}
void FixedUpdate ()
{
/* ----- BASIC XY PLANE moveDirectional ----- */
float hMove = Input.GetAxisRaw ("Horizontal");
float vMove = Input.GetAxisRaw ("Vertical");
if (hMove != 0 || vMove != 0)
{
}
/* ----- JUMPING ----- */
if ( (RayLanded() && Input.GetKey("space")) )
player_rb.velocity = Vector3.up * jumpSpeed * Time.deltaTime;
/* ----- MOUSE AIM ----- */
// Lock and hide the cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
float hMouse = Input.GetAxisRaw ("Mouse X");
float vMouse = Input.GetAxisRaw ("Mouse Y");
float hRotation = hMouse * sensitivity * Time.deltaTime;
// float vRotation = vMouse * sensitivity * Time.deltaTime;
transform.Rotate(0f,hRotation,0f);
}
/* ----- Other functions ----- */
// Checks if the player is grounded
bool RayLanded()
{
Ray jumpDetect = new Ray(player_trans.position,Vector3.down);
RaycastHit hitGround;
if (Physics.Raycast (jumpDetect, out hitGround, jumpMaxDistance))
{
return true;
}
return false;
}
// Checks if the player has pressed the Use key
public bool isInteracting()
{
if (Input.GetKey ("e"))
return true;
else
return false;
}
}
I guess it’s not a code gem, but bear with me, I’m just starting out. Also, ignore some of the other functions, it’s all pretty experimental. Also, I know there is a character controller, but I’d like to build the controller myself as it helps me understand what I’m doing better.
I’ve thought of many different solutions: transforming from local to global coordinates, defining my own ‘forward vector’ and detect direction through angles, but I can’t think it through. Any suggestions are welcome. Thank you for your time!