Help with Script to jump in Unity 3D

-2

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");

}



}

}
    
asked by Aymanete 27.11.2018 в 20:37
source

1 answer

0

Analyzing the section of your code I can make an observation.

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);
}

In this section of specific code it is convenient that when establishing new values for the speed of an object in a Vector3 the values that do not want to be modified in the following way are conserved.

movement.Set(h,  playerRigidbody.velocity.y , v);

What happens is that you add force in and , but the next time you update you send to call the function again and you add 0f to its axis and again, which prevents it from moving , because the update is to almost instantly.

-Gremin

    
answered by 02.12.2018 в 02:00