- Home /
Question by
irokaiser · Jun 01, 2020 at 05:32 PM ·
movementrigidbodycamera-movementjumping
Help with player movement and jumping
Hi, I have a basic camera system using Cinemachine and I am having problems with my jumping when I use the Kinematic tickbox as in it doesn't work. However, if I don't tick the Kinematic tickbox I get all of these weird camera flickers and glitches as shown in this video
This is the code for my player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController Controller;
public Transform Camera;
public Rigidbody RB;
public float Speed = 6f;
public float TurnSmoothTime = 0.15f;
float turnSmoothVelocity;
public float JumpForce = 5f;
bool CanJump = true;
private void Start()
{
RB = GetComponent<Rigidbody>();
}
// 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 (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + Camera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, TurnSmoothTime);
transform.rotation = Quaternion.Euler(0, angle, 0);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
Controller.Move(moveDir.normalized * Speed * Time.deltaTime);
}
//Jumping
if (Input.GetButtonDown("Jump") && CanJump)
{
RB.AddForce(new Vector3(0, JumpForce, 0), ForceMode.Impulse);
CanJump = false;
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Platform")
{
CanJump = true;
}
}
}
Please help :(
Comment