Isometric Survival Shooter Tutorial - How do I add jump?
I just completed the excellent tutorial for creating an isometric shooter. I really want to build on this and the first thing I'd like to do is add the ability to jump.
I looked up jump code and I tried to implement it but I'm new so I'm doing it wrong. For example, in the tutorial PlayerMovement.cs there's the public class PlayerMovement : Monobehavior. The code I found for adding jump has the public class `using UnityEngine. This is my code from the tutorial:
public class PlayerMovement : MonoBehaviour
{
public float speed = 6f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floorMask;
float camRayLength = 100f;
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);
}
void Move (float h, float v)
{
movement.Set (h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition (transform.position + movement);
}
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);
}
}
The code I'm trying to merge is:
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public bool grounded = true;
public float jumpPower = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Jump") && grounded == true) {
rigidbody2D.AddForce(transform.up*jumpPower);
grounded = false;
}
}
}
I realize the jump code needs to set grounded back to true to make it work correctly but I figured I'd address this as an extra step once I got the basic code working.
I have tried to merge the code two different ways but, as expected, I received errors in both instances. Any help would be greatly appreciated!
Thank You!
A friend of $$anonymous$$e told me I didn't have to merge this code but write a separate Jump code. I'm going to try that too.
-Bryan
Your answer
![](https://koobas.hobune.stream/wayback/20220612040810im_/https://answers.unity.com/themes/thub/images/avi.jpg)