using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewTheirdPersonn : MonoBehaviour
{
//public
public CharacterController controller;
public Transform cam;
public float myGravity = 9.81f;
public float speed = 6f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
//private
public Animation Anim;
private Animator Animator;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
if (!controller.isGrounded)
rb.AddRelativeForce(Vector3.down * myGravity);
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (!controller.isGrounded)
direction = new Vector3(horizontal, 0f, vertical);
if (direction.magnitude >= 0.1f)
{
float targetAngel = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngel, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngel, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
}
here I do have a Skript vor my movement and the traking for my third person camera. Now I have the problem that my Gravity doesent work very well. The problem is when I move my and if I am over a spot that is lower that the spawnpoint then the character in my game flys over the spot if I still hold my Key “W”. If I let it go the character drops on the Layer down. But if I hold “W” again it goes on to the same Y koordinates like the Spawnpoint and the Character Flys again. Does anyone have an idea what in the code has to be changed to fix this problem?