Camera Controller Script - locked Y Axis
Hello all, I'm relatively new to Unity and I've been working on writing my own character and camera controller scripts. I've watched a few different videos and I've been able to cobble together some code that does essentially what I want, aside from the fact that I would like the Y axis to be controlled by mouse movement while also being horizontally locked to the game object. The following is my CharacterController Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CharacterController : MonoBehaviour {
[System.Serializable]
public class MoveSettings
{
public float forwardVel = 12;
public float leftVel = 8;
public float rightVel = 8;
public float strafeVel = 12;
public float rotateVel = 100;
public float jumpVel = 5;
public float distToGrounded = 0.6f;
public float controlDownAcceleration = 2;
public LayerMask ground;
}
[System.Serializable]
public class PhysSettings
{
public float downAccel = .5f;
public float controlDownAccel = 5f;
}
[System.Serializable]
public class InputSettings
{
public float inputDelay = 0.1f;
public string FORWARD_AXIS = "Vertical";
public string TURN_AXIS = "Horizontal";
public string JUMP_AXIS = "Jump";
public string MOUSE_AXIS_X = "Mouse X";
public string MOUSE_AXIS_Y = "Mouse Y";
public string FALL_AXIS = "Downwards";
}
public MoveSettings moveSetting = new MoveSettings();
public PhysSettings physSetting = new PhysSettings();
public InputSettings inputSetting = new InputSettings();
Vector3 velocity = Vector3.zero;
public Quaternion targetRotation;
Rigidbody rBody;
float forwardInput, turnInput, jumpInput, mouseInputX, mouseInputY, downwardsInput;
public Quaternion TargetRotation
{
get { return targetRotation; }
}
bool Grounded()
{
return Physics.Raycast(transform.position, Vector3.down, moveSetting.distToGrounded, moveSetting.ground);
}
void Start()
{
targetRotation = transform.rotation;
if (GetComponent<Rigidbody>())
rBody = GetComponent<Rigidbody>();
else
Debug.LogError("This character needs a rigidbody.");
forwardInput = turnInput = jumpInput = 0;
}
void GetInput()
{
forwardInput = Input.GetAxis(inputSetting.FORWARD_AXIS);
turnInput = Input.GetAxisRaw(inputSetting.TURN_AXIS); //get axis interpolates -1 to beyond
mouseInputX = Input.GetAxis(inputSetting.MOUSE_AXIS_X);
mouseInputY = Input.GetAxis(inputSetting.MOUSE_AXIS_Y);
downwardsInput = Input.GetAxis(inputSetting.FALL_AXIS);
jumpInput = Input.GetAxisRaw(inputSetting.JUMP_AXIS); // not interpolated (return 1, -1, 0)
}
void Update()
{
GetInput();
Turn();
}
void FixedUpdate()
{
Run();
Jump();
Strafe();
rBody.velocity = transform.TransformDirection(velocity);
}
void Run()
{
if (Mathf.Abs(forwardInput) > inputSetting.inputDelay)
{
//move
velocity.z = moveSetting.forwardVel * forwardInput;
}
else
{
velocity.z = 0;
}
}
void Turn()
{
if (Input.GetAxis("Mouse X") > 0)
{
targetRotation *= Quaternion.AngleAxis(moveSetting.rotateVel * mouseInputX * Time.deltaTime, Vector3.up);
}
else if (Input.GetAxis("Mouse X") < 0)
{
targetRotation *= Quaternion.AngleAxis(moveSetting.rotateVel * mouseInputX * Time.deltaTime, Vector3.up);
}
transform.rotation = targetRotation;
}
void Jump()
{
if (jumpInput > 0 && Grounded() )
{
//jump
velocity.y = moveSetting.jumpVel;
}
else if (jumpInput == 0 && Grounded())
{
//zero out velocity
velocity.y = 0;
}
else if (jumpInput > 0 && !Grounded())
{
velocity.y++;
}
else if (downwardsInput > 0 && !Grounded())
{
Debug.Log("You have pressed shift");
velocity.y--;
}
else
{
//decrease velocity.y
velocity.y -= physSetting.downAccel;
}
}
void Strafe()
{
if (Input.GetKey(KeyCode.A))
{
velocity.x = moveSetting.strafeVel * -1;
}
else if (Input.GetKey(KeyCode.D))
{
velocity.x = moveSetting.strafeVel;
}
else
{
velocity.x = 0;
}
}
}
The CharacterController script is working as intended I just wanted to include the code just in case. The next chunk is my FollowTarget script that is the standard asset and I just changed it for my uses. What I want the camera to do is update the Y axis with Mouse Y input while it also is facing the same direction as the GameObject, the player, it is nested inside of. Currently it just locks the Y axis and transform.forward matches target.forward. using System; using UnityEngine;
namespace UnityStandardAssets.Utility
{
public class FollowTarget : MonoBehaviour
{
private Camera camera;
public Transform target;
public Vector3 yAxisVector;
public float yAxisValue;
public Vector3 offset = new Vector3(0f, 7.5f, 0f);
public float smooth = 2.0f;
void Update()
{
if (Input.GetAxis("Mouse Y") > 0)
{
Debug.Log("Greater than zero!");
}
}
private void LateUpdate()
{
transform.position = target.position + offset; // set camera position relative to playerobject
transform.forward = target.forward; // makes camera face the playerobject's direction but locks Y axis
}
}
}
I've narrowed the problem down to the transform.forward = target.forward line but I don't know of a way around the issue that I'm facing. Any help would be appreciated!! Thanks in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220612070441im_/https://answers.unity.com/themes/thub/images/avi.jpg)