Question by
Kamil11101 · May 23, 2018 at 07:50 PM ·
collisionmovement scriptjumping
Problem with moving and colliding
Hi , I recently tried to do FPS movement script + camera for this. But I can't make my player jump and if I add Rigid Body my character don't even move at all. I'm not even sure about that movement script...There is any good way to do script for moving in FPS game? Player Movement Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 6f;
public float gravity = 9f;
public bool isGrounded = true;
private Rigidbody rb;
public CapsuleCollider col;
float jumpForce = 15f;
private CharacterController charCont;
void Start ()
{
charCont = GetComponent<CharacterController> ();
rb = GetComponent<Rigidbody> ();
col = GetComponent<CapsuleCollider> ();
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
hit.gameObject.transform.position = Vector3.zero;
}
void Update ()
{
if (isGrounded == true) {
float deltaX = Input.GetAxis ("Horizontal") * speed;
float deltaZ = Input.GetAxis ("Vertical") * speed;
Vector3 movem = new Vector3 (deltaX, 0, deltaZ);
movem = Vector3.ClampMagnitude (movem, speed);
//movem.y = gravity;
movem *= Time.deltaTime;
movem = transform.TransformDirection (movem);
charCont.Move (movem);
if (Input.GetKeyDown (KeyCode.Space))
rb.AddForce (Vector3.up * jumpForce, ForceMode.Impulse);
} else {
return;
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Ground")
isGrounded = true;
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.tag == "Ground") {
isGrounded = false;
}
}
}
Comment
Your answer
