Hi there! I’m working on my first project currently and am having a lot of fun with it. I have been able to successfully get it to work like I want it to so far. The idea is that you’re able to control a motorcycle by driving it forward/backward (Up/Down arrows), turn the bike left and right with those arrow keys, and go at a faster speed by holding down the shift button. The bike will also tilt if you’re driving and turning at the same time. I have a separate script that is making the camera follow behind the bike, but here’s the code for the bike itself:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BikeScript : MonoBehaviour
{
public Transform bodyTransform;
public Transform wheelTransform1;
public Transform wheelTransform2;
public float moveSpeed = 30f;
public float normMoveSpeed = 30f;
public float turboMoveSpeed = 45f;
public float dirRotSpd = 80f;
public float normDirRotSpd = 80f;
public float turboDirRotSpd = 160f;
public float leanAmount = 30f;
public float leanSpeed = 10f;
// Start is called before the first frame update
void Start()
{
bodyTransform.parent = transform;
wheelTransform1.parent = bodyTransform;
wheelTransform2.parent = bodyTransform;
}
// Update is called once per frame
void Update()
{
PlayerMovement();
}
void PlayerMovement()
{
//Gets input
float hor = Input.GetAxisRaw("Horizontal");
float ver = Input.GetAxisRaw("Vertical");
//Checking for if the shift key is held down
if (Input.GetKey(KeyCode.LeftShift))
{
moveSpeed = turboMoveSpeed;
dirRotSpd = turboDirRotSpd;
}
else
{
moveSpeed = normMoveSpeed;
dirRotSpd = normDirRotSpd;
}
//Moves the player forward/backwards
Vector3 playerMovement = new Vector3(0f, 0f, ver) * moveSpeed * Time.deltaTime;
transform.Translate(playerMovement, Space.Self);
//Turns the player left/right
Vector3 playerTurn = new Vector3(0f, hor, 0f) * dirRotSpd * Time.deltaTime;
transform.Rotate(playerTurn, Space.Self);
//Leans the bike in the direction it's turning while moving
Quaternion currentLean = bodyTransform.rotation;
Quaternion normLean = transform.rotation;
Vector3 v = bodyTransform.rotation.eulerAngles;
if (hor != 0 && ver != 0)
{
if (hor == 1)
{
Quaternion desiredLean = Quaternion.Euler(v.x, v.y, -leanAmount);
bodyTransform.rotation = Quaternion.Slerp(bodyTransform.rotation, desiredLean, leanSpeed * Time.deltaTime);
} else {
Quaternion desiredLean = Quaternion.Euler(v.x, v.y, leanAmount);
bodyTransform.rotation = Quaternion.Slerp(bodyTransform.rotation, desiredLean, leanSpeed * Time.deltaTime);
}
} else {
bodyTransform.rotation = Quaternion.Slerp(bodyTransform.rotation, normLean, leanSpeed * Time.deltaTime);
}
}
}
Like I said, it works how I want it to. My concern at the moment is I feel like there’s too much going on in the PlayerMovement function. I feel like ideally I would break it up into functions for PlayerInput, BikeMovement, and BikeLean. I wanted to ask some more experienced programmers how they would suggest breaking this code up? Do you think it’s reasonable to have them in one function? Multiple functions? Maybe even separating them into there own scripts? If I were to separate the player input from the other stuff going on in the PlayerMovement function, I don’t know how I would get those numbers to the other functions. I might try and have the PlayerInput function return those values to the other functions, but I don’t know how I would return multiple numbers like that. Anyways, any input on this is appreciated! And if there’s anything about the code that I could change about the code for the better, let me know about that as well. Thanks in advance!