Question by
SneekySoprano · Jun 01, 2020 at 01:15 PM ·
unity 5movementphysicsplayer
Jumping and collision issue
I made this simple code for my player movement, but jumping looks weird it just teleport to the top and then strangely fall. What should I change to make it look good? Also i have a i have a little issue with collision, after walking into cube I can't jump anymore.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float moveSpeed = 3f;
public float jumpSpeed = 2f;
bool isGrounded = true;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if(Input.GetKey(KeyCode.W))
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.S))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.A))
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.D))
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
if(isGrounded == true && Input.GetKey(KeyCode.Space))
transform.Translate(Vector3.up * jumpSpeed * Time.deltaTime);
}
void OnCollisionEnter(Collision theCollision)
{
if(theCollision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit(Collision theCollision)
{
if(theCollision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
}
Comment
Answer by SneekySoprano · Jun 01, 2020 at 03:55 PM
I changed
transform.Translate(Vector3.up * jumpSpeed * Time.deltaTime);
to
rb.AddForce(0, jumpSpeed, 0);
and it worked, but i still have this collision issues. :(