My player spins when I enter play mode. What am I missing?
When I enter Play Mode, my player spins in place. It is a 3D spaceflight/space shooter game. I can still move it via my input settings, but as it moves it continues to rotate. I have the code for the ShipController and Inspector screenshots. Please advise.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipController : MonoBehaviour
{
public float forwardSpeed = 25f, strafeSpeed = 7.5f, hoverSpeed = 5f;
private float activeForwardSpeed, activeStrafeSpeed, activeHoverSpeed;
private float forwardAcceleration = 2.5f, strafeAcceleration = 2f, hoverAcceleration = 2f;
private float lookRateSpeed = 90f;
public 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 * 0.5f;
screenCenter.y = Screen.height * 0.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.GetAxisRaw("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);
}
}
this line makes no sense to me
rollInput = $$anonymous$$athf.Lerp(rollInput, Input.GetAxisRaw("Roll"), rollAcceleration * Time.deltaTime);
start by removing Time.deltaTime there makes no sense, you will need to decrease rollAcceleration value
I tried to comment out the line, removed 'Time.deltaTime' and decreased my rollAcceleration to 0. I am still having the same problem.
From the tutorial I followed, that line was to allow for the spacecraft to roll as another maneuver.
If the tutorial told you to use
rollAcceleration * Time.deltaTime
Consider following other tutorial. Anyway have you tried to remove this 3 lines to know which one is giving you issues?
transform.Rotate(-mouseDistance.y * lookRateSpeed * Time.deltaTime, mouseDistance.x * lookRateSpeed * Time.deltaTime, rollInput * rollSpeed * Time.deltaTime, Space.Self);
transform.position += transform.forward * activeForwardSpeed * Time.deltaTime;
transform.position += (transform.right * activeStrafeSpeed * Time.deltaTime) + (transform.up * activeHoverSpeed * Time.deltaTime);