Hi Together,
i am working on a Top-Down Game and i try to implement a kind of gliding functionality. If i jump off a cliff my character starts gliding and slowly sinking. The character moves automatically forward and he is flying in the direction of the joystick. When i do nothing the player is flying straight forward to the actual facingDirection. What i want is that the character tilts left or right if i change the direction. The stronger the direction change is the stronger the tilt. Like in a short hike (a game everyone should try ).
But i just dont get it how i could do that. Maybe someone can give me a hint. Here my actual code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : MonoBehaviour
{
private float movementSpeed;
public float baseSpeed = 200.0f;
public float Sensitivity = 5.0f;
public float rotationSpeed = 5.0f;
[HideInInspector] public Vector3 movingDirection = Vector3.zero;
private Vector3 horizontalDirection;
private Vector3 verticalDirection;
private Quaternion facingDirection;
private Rigidbody rigidbodyPlayer;
private Vector3 right, forward;
public float multiplier = 10.0f;
void Awake()
{
rigidbodyPlayer = GetComponent<Rigidbody>();
}
private void Start()
{
forward = Camera.main.transform.forward;
forward.y = 0;
forward = Vector3.Normalize(forward);
right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
facingDirection = transform.rotation;
}
void Update()
{
GetMovementDirection();
}
void FixedUpdate()
{
MoveCharacter();
}
//Get the Input for the Movement
private void GetMovementDirection()
{
horizontalDirection = right * Input.GetAxis("Horizontal");
verticalDirection = forward * Input.GetAxis("Vertical");
movingDirection = Vector3.Normalize(horizontalDirection + verticalDirection);
//Speed of movement multiplied by sensitivity for better control
movementSpeed = Sensitivity * (Mathf.Clamp01(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")).sqrMagnitude));
}
//Move the character according to GetMovementDirection
private void MoveCharacter()
{
//Automatic movement and direction change
rigidbodyPlayer.AddForce(transform.forward * baseSpeed);
rigidbodyPlayer.MovePosition(rigidbodyPlayer.position + movingDirection * Time.fixedDeltaTime);
//Smooth Rotation to moving direction
if (movingDirection != Vector3.zero)
{
facingDirection = Quaternion.LookRotation(movingDirection);
}
transform.rotation = Quaternion.Slerp(transform.rotation, facingDirection, Time.deltaTime * movementSpeed);
}
}