I find these to be rage inducing since i can’t fix the issues for a few days now, and i am pretty sure i did try every solution that i found.
I have 3 problems:
- player character briefly (0.5 sec) stops when drastically changing direction
- player character catches on wall edges, and sometimes to walls themselves
- i am able to change direction when sliding off the ‘ground’
Since I know how annoying it can be when people ask for their things to be fixed for them, so I will attach the project package with all the stuff in it so you guys can inspect it and help me fix the issues. I will be very grateful to anyone who decides to help me fix these problems.
2741372–197013–Arena.unitypackage (60 KB)
I managed to get stuff working mostly, but I still have that annoying issue of player humping walls when i try to jump while holding down one of movement keys and jumping. How do I fix this?
this is the script I am using right now, you can test it with my package from above post, just replace PlayerCtrls.cs script
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (CapsuleCollider))]
public class PlayerCtrls : MonoBehaviour
{
public float speed = 1000.0f;
private float MoveHorizontal, MoveVertical;
private Rigidbody rb;
private Vector3 PlayerMovement;
private Vector3 MoveDir2D;
private Vector3 MoveDir3D;
private Vector3 GunAim;
private GameObject PlayerGunObj;
private RaycastHit RayHit;
private float RayDistance = 0.5f;
bool isGrounded = true;
void Start()
{
PlayerMovement = Vector3.zero;
MoveDir2D = Vector3.zero;
rb = GetComponent<Rigidbody>();
PlayerGunObj = GameObject.Find("GunPivot");
}
void Update()
{
//get player's currently facing direction (without vertical axis)
MoveDir2D = new Vector3(Camera.main.transform.forward.x, 0, Camera.main.transform.forward.z).normalized;
transform.forward = MoveDir2D;
//rotate gun in player's currently facing direction according to camera angle
GunAim = Vector3.RotateTowards(PlayerGunObj.transform.forward, Camera.main.transform.forward, speed, 0); // speed only? or maybe speed * Time.deltatime???
PlayerGunObj.transform.rotation = Quaternion.LookRotation(GunAim);
// move player forward
if(Input.GetKey(KeyCode.W))
rb.AddForce(transform.forward * speed * Time.deltaTime);
// move player backward
if (Input.GetKey(KeyCode.S))
rb.AddForce(-(transform.forward) * speed * Time.deltaTime);
// move player left
if (Input.GetKey(KeyCode.A))
rb.AddForce(-(Camera.main.transform.right) * speed * Time.deltaTime);
// move player right
if (Input.GetKey(KeyCode.D))
rb.AddForce(Camera.main.transform.right * speed * Time.deltaTime);
// move if direction is combined
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) ||
Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
{
rb.AddForce(transform.forward * (speed / 8 * Time.deltaTime)); // ??? not sure about this math here - seems to give desired result
}
// stop moving if movement keys are released
// this also causes the player to briefly stop in place if combination of direction keys is held down and than one gets released
if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D))
{
if (rb.velocity.magnitude > 0)
{
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
}
// stop moving if opposing direction keys are pressed
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D))
{
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
// jump
if (Input.GetKeyDown("space"))
{
PlayerJump();
}
// jump in a direction
if (Input.GetKeyDown("space") && Input.GetKey(KeyCode.W) || Input.GetKeyDown("space") && Input.GetKey(KeyCode.S) ||
Input.GetKeyDown("space") && Input.GetKey(KeyCode.A) || Input.GetKeyDown("space") && Input.GetKey(KeyCode.D))
{
PlayerJump();
// possible air control implementation here
}
Debug.DrawRay(transform.position, MoveDir2D, Color.green);
}
void PlayerJump()
{
if(isGrounded == true)
rb.AddForce(0, 250, 0);
}
void OnCollisionStay(Collision collider)
{
CheckIfGrounded ();
}
void OnCollisionExit(Collision collider)
{
isGrounded = false;
}
void CheckIfGrounded()
{
if (Physics.Raycast(rb.transform.position, Vector3.down, out RayHit))
{
isGrounded = true;
}
}
}