The thing is that I'm very new and I'm doing a game for a race subject and I can not find a way to make the damn Player jump. It moves on the X and Z axes, but I can not get it to jump. I would greatly appreciate if someone finds me the fault. On the other hand, it does not pay attention to the part of the code that sends it to rotate depending on where the mouse points. This is secondary, if they solve it I would appreciate it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed=15.0f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floorMask;
float CamRayLength = 100f;
private void Awake()
{
floorMask = LayerMask.GetMask("Floor");
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
Turning();
Animating(h, v);
}
I try to jump with this code, but it does not work
void Update()
{
if (Input.GetButtonDown("Jump"))
playerRigidbody.AddForce(Vector3.up * 100);
}
void Move(float h, float v)
{
movement.Set(h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition(transform.position + movement);
}
This is the part that is supposed to make the player rotate according to where the mouse points
void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
if (Physics.Raycast(camRay, out floorHit, CamRayLength, floorMask))
{
Vector3 playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
playerRigidbody.MoveRotation(newRotation);
}
}
void Animating(float h, float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool("IsWalking", walking);
if (Input.GetButtonDown("Jump"))
{
anim.SetTrigger("Jumping");
}
}
}