Hey all, I’m new to coding and don’t really understand why my code (from a tutorial) isn’t working as intended - any help would be so appreciated!
The intent is to make mouse movement control where the ship is looking (it’s pitch and rotate), but for some reason the mouse X axis is working and the Y is instead controlling left/right roll.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmallShipController : MonoBehaviour
{
public float forwardSpeed = 25f, strafeSpeed = 7.5f, hoverSpeed = 5f;
private float activeForwardSpeed, activeStrafeSpeed, activeHoverSpeed;
private float forwardAcceleration = 2.5f, strafeAcceleration = 2f, hoverAcceleration = 2f;
public float lookRateSpeed = 90f;
private Vector2 lookInput, screenCenter, mouseDistance;
private float rollInput;
public float rollSpeed = 90f, rollAcceleration = 3.5f;
// Start is called before the first frame update
void Start()
{
screenCenter.x = Screen.width * .5f;
screenCenter.y = Screen.height * .5f;
Cursor.lockState = CursorLockMode.Confined;
}
// Update is called once per frame
void Update()
{
lookInput.x = Input.mousePosition.x;
lookInput.y = Input.mousePosition.y;
mouseDistance.x = (lookInput.x - screenCenter.x) / screenCenter.y;
mouseDistance.y = (lookInput.y - screenCenter.y) / screenCenter.y;
mouseDistance = Vector2.ClampMagnitude(mouseDistance, 1f);
rollInput = Mathf.Lerp(rollInput, Input.GetAxis("Roll"), rollAcceleration * Time.deltaTime);
transform.Rotate(-mouseDistance.y * lookRateSpeed * Time.deltaTime, mouseDistance.x * lookRateSpeed * Time.deltaTime, rollInput * rollSpeed * Time.deltaTime, Space.Self);
activeForwardSpeed = Mathf.Lerp(activeForwardSpeed, Input.GetAxisRaw("Vertical") * forwardSpeed, forwardAcceleration * Time.deltaTime);
activeStrafeSpeed = Mathf.Lerp(activeStrafeSpeed, Input.GetAxisRaw("Horizontal") * strafeSpeed, strafeAcceleration * Time.deltaTime);
activeHoverSpeed = Mathf.Lerp(activeHoverSpeed, Input.GetAxisRaw("Hover") * hoverSpeed, hoverAcceleration * Time.deltaTime);
transform.position += transform.forward * activeForwardSpeed * Time.deltaTime;
transform.position += (transform.right * activeStrafeSpeed * Time.deltaTime) + (transform.up * activeHoverSpeed * Time.deltaTime);
}
}
My Input Manager is set up correctly I believe
Hopefully it’s a simple fix, and thank you!