- Home /
3D Object Jump Glitch
Alright, so after following a few tutorials online about creating an object pickup/ throw script, and implementing a jump mechanic using the character controller, I have this glitch where if the object that the player is holding gets too close to the players ground check (which regulates jumping based on whether or not the player is on ground - also works on objects if they are classified as the ground layer) you can basically infini-jump so long as your looking down. I just want to note that the pickup script checks the distance between the object and the player, and if it goes too far it drops the item, but it is still able to be moved around within that range by knocking it into other objects w collision.
I'm looking for a clever fix, but I'll list some potential solutions below that I've thought followed by the reason why they wont work.
Solution 1: Make the player drop the item being held if it gets too close (Doesn't work because my script checks the distance between the exact point on the object you look at before picking it up, so if you look at the farther end of the object, it can get close enough to trigger the glitch still). This is the only solution to work in the slightest, but like I said it depends on which part of the item you pick up
Solution 2: Make it so that the object is in a fixed position when picking it up, meaning if it collides with another object it wont budge (This one won't work because I'm fairly sure that if I look down at the ground while picking it up, it'll prop my player up, which is counter intuitive obviously. I could be wrong but I don't want to waste time testing it lol)
Solution 3: Have jumping cool-downs (This just wont work because then it punishes the player for being skillful in their movement. My game will be heavily reliant on precise movements so I'd rather look for another fix.)
Essentially what I have pictured for my game is a valve-esque kind of mini game which is kind of like a game of tag, more competitive in the sense that it rewards players for hiding, and penalizes those who are it for not tagging another player. The reason I mention valve is because i really like the flow of movement in games like CS:GO, Half Life, and Portal. Below ill attach some code to better portray what I've mentioned above, hopefully someone will see this and feel free to help. Thanks to those who took the time to even read this :)
$$anonymous$$y pickup and throw script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PickUpAndThrow : $$anonymous$$onoBehaviour {
float throwForce = 18000;
Vector3 objectPos;
float distance;
public bool canHold = true;
public GameObject item;
public GameObject tempParent;
public bool isHolding = false;
// Update is called once per frame
void Update()
{
distance = Vector3.Distance(item.transform.position, tempParent.transform.position);
if (distance >= 5f || distance <= 1.5f)
{
isHolding = false;
}
//Check if isholding
if (isHolding == true)
{
item.GetComponent<Rigidbody>().velocity = Vector3.zero;
item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
item.transform.SetParent(tempParent.transform);
if (Input.Get$$anonymous$$ouseButtonDown(1))
{
item.GetComponent<Rigidbody>().AddForce(tempParent.transform.forward * throwForce);
isHolding = false;
}
}
else
{
objectPos = item.transform.position;
item.transform.SetParent(null);
item.GetComponent<Rigidbody>().useGravity = true;
item.transform.position = objectPos;
}
/////////////////////////////////////////////////////////////
/*
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
if (distance <= 5f && distance >= 2f)
{
isHolding = true;
item.GetComponent<Rigidbody>().useGravity = false;
item.GetComponent<Rigidbody>().detectCollisions = true;
}
}
if (Input.Get$$anonymous$$ouseButtonDown(0) && isHolding)
{
isHolding = false;
}
*/
}
void On$$anonymous$$ouseDown()
{
if (distance <= 5f && distance >=1.5f)
{
isHolding = true;
item.GetComponent<Rigidbody>().useGravity = false;
item.GetComponent<Rigidbody>().detectCollisions = true;
}
}
void On$$anonymous$$ouseUp()
{
isHolding = false;
}
}
$$anonymous$$ovement Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class $$anonymous$$ovement : $$anonymous$$onoBehaviour { public CharacterController controller; public Transform playerHeight; //does nothing lol
public float speed = 16f;
public float gravity = -50f;
public float jumpHeight = 10f;
bool isCrouching;
public Transform groundCheck;
public float groundDistance = 0.7f;
public Layer$$anonymous$$ask ground$$anonymous$$ask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
//Crouching mechanic
if (Input.GetKey(KeyCode.LeftControl))
{
isCrouching = true;
controller.height = 1.80f; //makes player ~60% of initial height
}
else
{
isCrouching = false;
controller.height = 3.18f;//makes player reg height again
speed = 16f;
}
//Checks if ur touching the ground while crouching, to slow you down.
if (isGrounded && isCrouching)
{
speed = 8f; //slows player down to half movement speed if crouching
}
//Sprint $$anonymous$$echanic
if (Input.GetKey(KeyCode.LeftShift) && isCrouching == false && isGrounded)
{
speed = 24f; //speeds player up to 1.5x movement speed if running
}
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, ground$$anonymous$$ask); //declares isGrounded bool
if(isGrounded && velocity.y < 0)
{
velocity.y = -10f; //regulates velocity if ur standing on ground
}
float x = Input.GetAxis("Horizontal"); //deter$$anonymous$$es x axis placement
float z = Input.GetAxis("Vertical"); //deter$$anonymous$$es z axis placement
Vector3 move = transform.right * x + transform.forward * z; //gives player movement
controller.$$anonymous$$ove(move * speed * Time.deltaTime); //allows the player to move at a consistent speed (does not exceed framerate)
if (Input.GetButton("Jump") && isGrounded)
{
velocity.y = $$anonymous$$athf.Sqrt(jumpHeight * -2f * gravity); //jump equation
}
velocity.y += gravity * Time.deltaTime; //y velocity decreases over time if not grounded
controller.$$anonymous$$ove(velocity * Time.deltaTime); //regulates speed w framerate
}
}
Your answer
Follow this Question
Related Questions
In Air Movement 0 Answers
how to rotate one piece and make another move linearly? 0 Answers
Move while jump on board 0 Answers
Moving player in direciton camera is facing 1 Answer
ai jump over object 2 Answers