I tried to implement a simple movement to my object. My object moves perfectly fine but I’m having issues with its collision. When I keep on moving object towards the obstacle/ground, collision are jittery.
Here the code for what I’ve written.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TempScript : MonoBehaviour
{
(SerializeField) float walk_speed = 5f;
(SerializeField) float jump_speed = 10f;
(SerializeField) float max_up = 7f;
(SerializeField) float max_low = -1.5f;
void Update()
{
PlayerMovement();
}
void PlayerMovement()
{
if (Input.GetKey(KeyCode.W) && transform.position.y <= max_up)
transform.position = transform.position + Vector3.up * jump_speed * Time.deltaTime;
if (Input.GetKey(KeyCode.S) && transform.position.y >= max_low)
transform.position = transform.position + Vector3.down * jump_speed * Time.deltaTime;
if (Input.GetKey(KeyCode.D))
transform.position = transform.position + Vector3.right * walk_speed * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
transform.position = transform.position + Vector3.left * walk_speed * Time.deltaTime;
}
}
The cube goes below the ground and bounces right back up. making it look like its jittering.
what i want is movement should stop when its colliding with object. just like it happens with addForce method.
for now you can ignore max_up
and max_down
variable, it was just to make sure its in the camera.