Pushed Crate/Cube won't drop while pushing into the void
First of all, I copied the code from a Unity Sample documentary: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnControllerColliderHit.html I thought the code is perfect for the game but I was wrong and so as the sample given. The object is still floating even it's already on the air. I had to turn off the gravity of the player so it won't fall so I can focus on other excess but I applied the gravity on the pushing object. The cube I pushed I can still see the cube has a very tedious downward slide when it's on the void but the cube will absolutely fall if I stop pushing it. Here's a gif link of the demonstration: https://imgur.com/a/tauYTnf
My complete code I applied of my Player script inherited with a Monobehaviour:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed;
CharacterController controller;
float yEulerAngle = 0f;
float getTime, shootTimer, shootTimerLength, shootTimerLengthValueAdd;
bool canShoot = false;
public Transform bullet;
Bullet bulletScript;
float getHorizontal;
float getVertical;
public bool useNormalized;
public float rayDistance = 0.5f;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
getTime = 0;
shootTimer = 0;
shootTimerLength = 0;
shootTimerLengthValueAdd = .05f;
//float getHorizontal = Input.GetAxisRaw("Horizontal");
//float getVertical = Input.GetAxisRaw("Vertical");
if (transform.rotation.y == 0)
{
getHorizontal = 0;
getVertical = 1;
}
else
{
Debug.Log("CAUTION: Bullets won't move unless you move.");
}
}
// Update is called once per frame
void Update()
{
Vector3 moveDirection = new Vector3(Input.GetAxisRaw("Horizontal") * speed, 0f, Input.GetAxisRaw("Vertical") * speed);
if (useNormalized) moveDirection = moveDirection.normalized * speed;
//Normal
if (Input.GetAxisRaw("Vertical") == 1) yEulerAngle = 0f;
if (Input.GetAxisRaw("Vertical") == -1) yEulerAngle = 180f;
if (Input.GetAxisRaw("Horizontal") == -1) yEulerAngle = 270f;
if (Input.GetAxisRaw("Horizontal") == 1) yEulerAngle = 90f;
//Diagonal
if (Input.GetAxisRaw("Vertical") == 1 && Input.GetAxisRaw("Horizontal") == 1) yEulerAngle = 45f;
if (Input.GetAxisRaw("Vertical") == 1 && Input.GetAxisRaw("Horizontal") == -1) yEulerAngle = 315f;
if (Input.GetAxisRaw("Vertical") == -1 && Input.GetAxisRaw("Horizontal") == 1) yEulerAngle = 135f;
if (Input.GetAxisRaw("Vertical") == -1 && Input.GetAxisRaw("Horizontal") == -1) yEulerAngle = 225f;
//Normal
if (Input.GetAxisRaw("Vertical") == 1)
{
getHorizontal = 0;
getVertical = 1;
}
if (Input.GetAxisRaw("Vertical") == -1)
{
getHorizontal = 0;
getVertical = -1;
}
if (Input.GetAxisRaw("Horizontal") == -1)
{
getHorizontal = -1;
getVertical = 0;
}
if (Input.GetAxisRaw("Horizontal") == 1)
{
getHorizontal = 1;
getVertical = 0;
}
//Diagonal
if (Input.GetAxisRaw("Vertical") == 1 && Input.GetAxisRaw("Horizontal") == 1)
{
getHorizontal = 1;
getVertical = 1;
}
if (Input.GetAxisRaw("Vertical") == 1 && Input.GetAxisRaw("Horizontal") == -1)
{
getHorizontal = -1;
getVertical = 1;
}
if (Input.GetAxisRaw("Vertical") == -1 && Input.GetAxisRaw("Horizontal") == 1)
{
getHorizontal = 1;
getVertical = -1;
}
if (Input.GetAxisRaw("Vertical") == -1 && Input.GetAxisRaw("Horizontal") == -1)
{
getHorizontal = -1;
getVertical = -1;
}
if (Input.GetAxisRaw("Submit") == 1)
{
//getTime =
//if(shootTimer >)/
//shootTimer = Time.time;
//shootTimerLength = Time.time + shootTimerLengthValueAdd;
//canShoot = false;
if (canShoot)
{
canShoot = false;
shootTimerLength = Time.time + shootTimerLengthValueAdd;
}
else
{
// shootTimer++;
}
//if shootTimer > shootTimerLength
if (shootTimerLength <= Time.time)
{
Transform muzzle = transform.Find("Muzzle");
bulletScript = bullet.GetComponent<Bullet>();
bulletScript.speed = new Vector3(getHorizontal * 16f, 0f, getVertical * 16f);
Instantiate(bullet, new Vector3(muzzle.position.x, muzzle.position.y, muzzle.position.z), Quaternion.identity);
canShoot = true;
}
}
transform.rotation = Quaternion.Euler(0f, yEulerAngle, 0f);
controller.Move(moveDirection * Time.deltaTime);
}
void FixedUpdate()
{
//controller.velocity = ;
}
// this script pushes all rigidbodies that the character touches
float pushPower = 2.5f;
void OnControllerColliderHit(ControllerColliderHit hit)
{
Debug.Log(hit.collider.name);
Rigidbody body = hit.collider.attachedRigidbody;
// no rigidbody
if (body == null || body.isKinematic)
{
return;
}
// We dont want to push objects below us
if (hit.moveDirection.y < -0.3)
{
return;
}
// Calculate push direction from move direction,
// we only push objects to the sides never up and down
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0f, hit.moveDirection.z);
// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.
// Apply the push
body.velocity = pushDir * pushPower;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612192259im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tilt character with acceleration 1 Answer
Error CS0103 0 Answers
First person movement with character controller does not detect ground properly 0 Answers
Skipping Movement when in fixed time? 0 Answers
Rope & Character controller ? 0 Answers