So I currently have a decent code, with a small Health system and player movement (with player rotation)
What I need is described by the title
I need the player to rotate his position towards every slope
Like if he was a snail, sticking to the walls!!
If it is possible, I will really appreciate to include a code to never leave the ground, I mean, no jumping when going fast on slopes, no getting of the ground
I am searching for a SNAIL behaviour
thankss!! this is my current code
//Variables
Vector3 velocity;
private CharacterController controller;
public float maxHealth = 100f;
private float health = 0f;
public float speed = 5f;
private float currentSpeed;
public float sprintSpeed = 12f;
public float gravity = 3f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
private bool playerIsAlive = true;
//Check basic stuff
void Start()
{
currentSpeed = speed;
health = maxHealth;
controller = GetComponent<CharacterController>();
}
//Update every frame, stuff
void Update()
{
if (playerIsAlive == true)
{
Move();
Health();
}
}
//Basic Move controller
void Move()
{
//Movement "Axis" set
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
//Direction set
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
//Gravity set
velocity.y -= gravity * Time.deltaTime;
if (direction.magnitude >= 0.1f && controller.isGrounded)
{
//Angle Basics
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
//Rotate to Direction Basics
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
//Move Basics
controller.Move(direction * currentSpeed * Time.deltaTime);
//Sprint Basics
if (Input.GetKey(KeyCode.Space))
{
currentSpeed = sprintSpeed;
}
else
{
currentSpeed = speed;
}
}
//Rotate Declaration
controller.Move(velocity * Time.deltaTime);
}
//Basic Health System
void Health()
{
if (health >= maxHealth)
{
health = maxHealth;
playerIsAlive = true;
}
else if (health <= 0)
{
Destroy(gameObject);
Debug.Log("Round Over, check results!");
playerIsAlive = false;
}
}