I am currently creating a game that requires an astronaut flying in space with its body tilting to the direction it is going to. I want the astronaut to tilt slightly (around 15 degrees) to the direction it’s going to. But the code I have written for it doesn’t work
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
public float upwardForce = 500f;
public float downwardForce = 500f;
public float tiltAmount = 15f; // Adjust the tilt amount as needed
private bool isTiltingW = false;
private bool isTiltingS = false;
void FixedUpdate()
{
// Ensure Rigidbody's rotation is not frozen
rb.freezeRotation = false;
// Apply forward force
rb.AddForce(transform.forward * forwardForce * Time.deltaTime);
// Apply sideways force based on user input
float horizontalInput = Input.GetAxis("Horizontal");
float horizontalForce = horizontalInput * sidewaysForce * Time.deltaTime;
rb.AddForce(transform.right * horizontalForce, ForceMode.VelocityChange);
// Tilt the player based on its velocity when "a" or "d" is pressed
if (isTiltingW)
{
float tilt = Mathf.Clamp(rb.velocity.y, -1f, 1f) * tiltAmount;
transform.localRotation = Quaternion.Euler(-tilt, 0, 0);
}
else if (isTiltingS)
{
float tilt = Mathf.Clamp(rb.velocity.y, -1f, 1f) * tiltAmount;
transform.localRotation = Quaternion.Euler(tilt, 0, 0);
}
else
{
// Smoothly return to the original rotation when not tilting
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, Time.deltaTime);
}
}
void Update()
{
// Detect if movement keys are pressed to enable tilt
isTiltingW = Input.GetKey("w");
isTiltingS = Input.GetKey("s");
}
}
Instead I am using this code but it only moves the astronaut without tilting.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
public float upwardForce = 500f; // Add upward force
public float downwardForce = 500f; // Add downward force
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
// Add upward force when the "w" key is pressed
if (Input.GetKey("w"))
{
rb.AddForce(0, upwardForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
// Add downward force when the "s" key is pressed
if (Input.GetKey("s"))
{
rb.AddForce(0, -downwardForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -100f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
Is there anyway to modify the last code for the astronaut to tilt 15 degrees to the directions is going to and once the player releases the button the astronaut goes back to its default position? I would be grateful if someone can help me with this code.