- Home /
Question by
unity_38167 · Jan 13, 2020 at 09:11 AM ·
c#walljump
Wall jumping
So far this is what I have for my movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9f;
public float jump = 3f;
private float jumpforce = 8;
private float verticalVelocity;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isgrounded;
// Update is called once per frame
void Update()
{
isgrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isgrounded && velocity.y < 0)
{
velocity.y = 0f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && isgrounded)
{
velocity.y = Mathf.Sqrt(jump * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (!controller.isGrounded && hit.normal.y < 0.1f)
{
if(Input.GetKey("c"))
Debug.DrawRay(hit.point, hit.normal, Color.red, 1.25f);
verticalVelocity = jumpforce;
}
}
}
the part at the bottom is for the wall jumping I'm trying to implement. Right now it only detects if I have hit the wall and pressed c, then it will draw the ray. I have no idea on how to move forward, my only guess was to do something like -> y = hit.normal * speed;
but don't know what to put for y.
If anyone knows what the problem is it would be greatly appreciated, thanks!
Comment