Question by
Sophia667 · Sep 05, 2018 at 08:03 AM ·
jumping objectpick uppick up objectcollectible
How to make 2 collectibles can only be reached by jumping in roll a ball tutorial
Hello everyone, I am new to unity community and codding. I already finished the roll a ball tutorial and now I am working on develop the game into two local players which have jumping functions. I already added jump keys for the players, but how to write a script to make that 2 collectible can only be reached by jumping? I am trapped here and hope to get some suggestions. Thank you very much!
here is part of my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Playercontroller : MonoBehaviour
{
public float speed;
public Text countText;
public KeyCode left;
public KeyCode right;
public KeyCode up;
public KeyCode down;
public KeyCode jump;
public bool isGrounded;
public float jumpHeight;
public float GravityStrength;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Playercontroller : MonoBehaviour
{
public float speed;
public Text countText;
public KeyCode left;
public KeyCode right;
public KeyCode up;
public KeyCode down;
public KeyCode jump;
public bool isGrounded;
public float jumpHeight;
public float GravityStrength;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
isGrounded = true;
count = 0;
SetCountText ();
}
void FixedUpdate()
{
Vector3 gravityS = new Vector3(0, GravityStrength,0);
Physics.gravity = gravityS;
if(Input.GetKey(jump) && isGrounded == true)
{
Jump();
}
}
void Jump()
{
rb.AddForce(new Vector3(0, jumpHeight, 0));
isGrounded = false;
}
void OnCollisionEnter()
{
isGrounded = true;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText();
}
}
}
}
void Start()
{
rb = GetComponent<Rigidbody>();
isGrounded = true;
count = 0;
SetCountText ();
}
{
rb.AddForce(new Vector3(0, jumpHeight, 0));
isGrounded = false;
}
void OnCollisionEnter()
{
isGrounded = true;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText();
}
}
Comment
Your answer
Follow this Question
Related Questions
How do I let the player pick up an item with left click? 0 Answers
Carrying an object using Javascript 0 Answers
collectibles you can read 0 Answers
Preventing munliple jumps 1 Answer
stick to roof of object when jumping 0 Answers