Im using a script that uses direction and deltaTime to make the character move with momentum. The problem is that its making the movement choppy. Any ideas how to fix?
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveForce = 500;
public float moveSpeed = .3f;
public float jumpForce = 500;
public Vector3 spawnpoint;
private Rigidbody rb;
private bool onGround = true;
// Start is called before the first frame update
void Start()
{
//VelocityYClamped = mathf.clamp(rb.velocity.y, 10, 30);
spawnpoint = transform.position;
rb = GetComponent();
}
// Update is called once per frame
void Update()
{
//float xMove = Input.GetAxis(“Horizontal”);
//float zMove = Input.GetAxis(“Vertical”);
Vector3 direction = Vector3.zero;
direction.x = Input.GetAxis(“Horizontal”);
direction.z = Input.GetAxis(“Vertical”);
direction.Normalize();
if( direction != Vector3.zero)
{
rb.MovePosition(transform.position + direction * moveSpeed * Time.deltaTime);
}
//transform.position += new Vector3(xMove, 0, zMove) * moveSpeed;
//rb.AddForce(new Vector3(xMove, 0, zMove) * moveForce);
if(Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
//rb.velocity = Vector3(rb.velocity.y, VelocityYClamped, rb.velocity.z);
private void Jump()
{
if(onGround)
{
rb.AddForce(new Vector3(0, 1, 0) * jumpForce);
onGround = false;
}
}
private void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == “Ground”)
{
onGround = true;
}
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == “Respawn”)
{
transform.position = spawnpoint;
}
}
}