Question by
TheRealManu · Dec 22, 2019 at 09:47 AM ·
3rd person camerawall jump
Help with wall run 3D
Hello im new to unity but i programmed a lot before in java. I´m now makind a 3D shooter and the wall run works fine but theres a problem: i can´t rotate the camera like you´re hanging on a wall. There is my code for movement:
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public Camera fpscam;
public float speed = 12f;
public float originalGravity = -9.81f;
private float gravity = 0;
public float jumpHeight = 3f;
public float crouch = 0.9f;
public float characterHeight = 2f;
bool isCrouch = false;
private RaycastHit hitR;
private RaycastHit hitL;
private bool isWallR = false;
private bool isWallL = false;
public Transform groundCheck;
public Transform body;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
private bool isGrounded;
private bool midAirJump = true;
private bool reseted = true;
// Start is called before the first frame update
void Start()
{
gravity = originalGravity;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
// Jump:
if(Input.GetButtonDown("Jump") && (isGrounded || midAirJump))
{
updateJump();
isCrouch = false;
checkCrouch();
midAirJump = false;
}
// Ground check:
if(isGrounded)
{
midAirJump = true;
gravity = originalGravity;
if (reseted == false)
{
fpscam.transform.localRotation = Quaternion.Euler(0, 0, 0);
reseted = true;
}
}
// Wall check:
if(!isWallL && !isWallR)
{
gravity = originalGravity;
}
// Wall check right:
if(Physics.Raycast(fpscam.transform.position, fpscam.transform.right, out hitR, 1f) && !isGrounded)
{
if(hitR.transform.tag == "Wall")
{
Debug.Log(hitR);
isWallR = true;
isWallL = false;
midAirJump = true;
gravity = 0f;
updateJump();
fpscam.transform.localRotation = Quaternion.Euler(15, 0, 0);
reseted = false;
}
} else
{
isWallR = false;
}
// Wall check left:
if (Physics.Raycast(fpscam.transform.position, -fpscam.transform.right, out hitL, 1f) && !isGrounded)
{
if(hitL.transform.tag == "Wall")
{
Debug.Log(hitL);
isWallL = true;
isWallR = false;
midAirJump = true;
gravity = 0f;
updateJump();
reseted = false;
fpscam.transform.localRotation = Quaternion.Euler(-15, 0, 0);
}
} else
{
isWallL = false;
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
// Crouch:
if(Input.GetKeyDown(KeyCode.LeftShift) && isGrounded)
{
isCrouch = !isCrouch;
checkCrouch();
}
}
void checkCrouch()
{
if(isCrouch)
{
speed = 6f;
controller.height = crouch;
body.localScale = new Vector3(1, crouch, 1);
} else
{
speed = 12f;
controller.height = characterHeight;
body.localScale = new Vector3(1, characterHeight, 1);
}
}
void updateJump()
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
And my code for looking around:
public class MouseLook : MonoBehaviour
{
public float mouseSensitivityY = 100f;
public float mouseSensitivityX = 200f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
I know its a bit chaotic but maybe you can help me out :)
Comment