- Home /
I followed Brackeys Third person camera tutorial on youtube,But my script doesn't work,I used Brackeys Third person camera movement tutorial youtube video
I converted the script from Character controller to rigidbody base Problems: 1:The player Jitters while moving 2:When i press Space key (Jump) I fly up and up 3:Unity's Rigidbody gravity somehow doesn't work
Here is my entire Playermovements script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovements : MonoBehaviour {
Rigidbody rb;
public Rigidbody Rigidbody;
public Transform Cam;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
public float Speed = 6f;
[SerializeField] float JumpForce = 3f;
[SerializeField] Transform groundCheck;
[SerializeField] LayerMask ground;
[SerializeField] AudioSource jumpSound;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
rb = GetComponent<Rigidbody>();
}
void Update()
{
//Horizontal & Verticle input
float Horizontal = Input.GetAxis("Horizontal");
float Vertical = Input.GetAxis("Vertical");
Vector3 Direction = new Vector3(Horizontal, rb.velocity.y,Vertical);
rb.velocity = new Vector3(Horizontal * Speed,rb.velocity.y,Vertical * Speed);
if (Direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(Direction.x, Direction.z) * Mathf.Rad2Deg + Cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y,targetAngle, ref turnSmoothVelocity,turnSmoothTime);
Vector3 moveDir = new Vector3(transform.forward.x, rb.velocity.y, transform.forward.z);
transform.rotation = Quaternion.Euler(0f,angle,0f);
rb.velocity = moveDir.normalized * Speed;
}
if (Input.GetButtonDown("Jump"))
{
Jump();
}
}
void Jump()
{
rb.velocity = new Vector3(rb.velocity.x,JumpForce,rb.velocity.z);
jumpSound.Play();
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy Head"))
{
Destroy(collision.transform.parent.gameObject);
Jump();
}
}
}
,
Comment
Your answer
Follow this Question
Related Questions
Two colliders triggering each other - how? 2 Answers
Freeze Position problem 1 Answer
Rigidbody.MovePosition doesn't move reliably? 1 Answer
adding drag to a relative force 0 Answers