objects are keep falling,
![Image][1]
![image][2]
![image][3]
![image][4]
![image][5]
I added some screenshots above. When I run the game obstacle are falling rapidly through y
axis. So, there's no more obstacle in that ground. If there's ground than those obstacle should stopped. I tried by freezing them. But, when another object hit them they are froze. So, I want ground to stop them. I have another question also. I have player movement. When I will run the program. The Player
should be go through z
axis. But, The object(player) don't move.
PlayerMovement.cs
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce=2000f;
void FixedUpdate()
{
rb.AddForce(0,0,forwardForce * Time.deltaTime);
if(Input.GetKey("d")){
rb.AddForce(30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}else if(Input.GetKey("a")){
rb.AddForce(-30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}
if(!Input.touchSupported)
{
// Still support PC keys
if(Input.GetKey(KeyCode.D))
{
rb.AddForce(30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}
else if(Input.GetKey(KeyCode.A))
{
rb.AddForce(-30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}
}
else
{
// For touch devices
if(Input.touchCount == 1)
{
var touch = Input.GetTouch(0);
var touchPos = touch.position;
var isRight = touchPos.x >= Screen.width / 2f;
if(isRight)
{
rb.AddForce(30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}
else
{
rb.AddForce(-30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}
}
}
if(Physics.Raycast(transform.position,Vector3.down,0.5f)==false){
// rb.velocity = new Vector3(0, rb.velocity.y, 0);
rb.constraints = RigidbodyConstraints.FreezePositionZ;
}else{
rb.constraints = RigidbodyConstraints.None;
}
if(rb.position.y < -1f){
FindObjectOfType<GameManager>().EndGame();
}
}
}
PlayerCollision.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movement;
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.name);
if (collisionInfo.collider.tag == "obstacle")
{
movement.enabled = false;
FindObjectOfType<GameManager>().EndGame();
}else if(collisionInfo.collider.name == "END"){
FindObjectOfType<GameManager>().CompleteLevel();
}
}
void Restart(){
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
![image][6]
If you look at above source code clearly. So, you might see that I have a Debug.Log
right there. But, when I run the game. I don't get any message to console.
As a new game developer(learning) I don't know what's happening, what's wrong with this.
I tried to stop those objects by freezing or unchecking use Gravity
or, checking Is Kinematic
. But, I want those obstacles to move if player
hits those objects or they are not on that ground.
,!First image][1]
![Second image][2]
![Third image][3]
![Forth image][4]
![Fifth image][5]
I added some screenshots above. When I run the game obstacle are falling rapidly through y
axis. So, there's no more obstacle in that ground. If there's ground than those obstacle should stopped. I tried by freezing them. But, when another object hit them they are froze. So, I want ground to stop them. I have another question also. I have player movement. When I will run the program. The Player
should be go through z
axis. But, The object(player) don't move.
PlayerMovement.cs
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce=2000f;
void FixedUpdate()
{
rb.AddForce(0,0,forwardForce * Time.deltaTime);
if(Input.GetKey("d")){
rb.AddForce(30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}else if(Input.GetKey("a")){
rb.AddForce(-30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}
if(!Input.touchSupported)
{
// Still support PC keys
if(Input.GetKey(KeyCode.D))
{
rb.AddForce(30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}
else if(Input.GetKey(KeyCode.A))
{
rb.AddForce(-30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}
}
else
{
// For touch devices
if(Input.touchCount == 1)
{
var touch = Input.GetTouch(0);
var touchPos = touch.position;
var isRight = touchPos.x >= Screen.width / 2f;
if(isRight)
{
rb.AddForce(30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}
else
{
rb.AddForce(-30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
}
}
}
if(Physics.Raycast(transform.position,Vector3.down,0.5f)==false){
// rb.velocity = new Vector3(0, rb.velocity.y, 0);
rb.constraints = RigidbodyConstraints.FreezePositionZ;
}else{
rb.constraints = RigidbodyConstraints.None;
}
if(rb.position.y < -1f){
FindObjectOfType<GameManager>().EndGame();
}
}
}
PlayerCollision.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movement;
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.name);
if (collisionInfo.collider.tag == "obstacle")
{
movement.enabled = false;
FindObjectOfType<GameManager>().EndGame();
}else if(collisionInfo.collider.name == "END"){
FindObjectOfType<GameManager>().CompleteLevel();
}
}
void Restart(){
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
![Sixth image][6]
If you look at above source code clearly. So, you might see that I have a Debug.Log
right there. But, when I run the game. I don't get any message to console.
As a new game developer(learning) I don't know what's happening, what's wrong with this.
I know I could stop those objects by uncheck Use gravity
or checking Is Kinematic
. But I want those obstacles to move if player
hits them. Or, if they are not on ground than those obstacle should be fall down.
Your answer
![](https://koobas.hobune.stream/wayback/20220613025137im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate sphere to the direction of movement 0 Answers
Ball won't stop bouncing 1 Answer
How to alter the bounce of a ball? Add force when hitting a ball? 0 Answers
Object falling very slow 0 Answers