Player Movement

After my obstacle touched the ground plane it didnt change colours but after I bump it using my player it starts moving upwards and then falls back down I freezed the rotations of my obstacles and I also froze the x and z movements but because I cant freeze the y movement of the obstacle whenever my player bumps into it it flies up and comes back down

Hi,

Are there any error messages in your console when the obstacle is supposed to change the colour? And have you already rewatched the video at least one more time?

I sent a mail to community@gamedev.tv since i couldnt upload videos here.
If you can check it it’ll be a great help.

Unfortunately, I don’t have access to that e-mail account. You could upload your videos to Youtube and share a link here.

sure


here

Thank you.

  1. Could you also please share your relevant code as formatted text?
  2. And could you share a screenshot of the Inspector of both the falling obstacle and the player?

ok

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Dropper : MonoBehaviour
{
Rigidbody rigidbody;
MeshRenderer renderer;

[SerializeField] float timeToWait = 2f;
void Start()
{
    renderer = GetComponent<MeshRenderer>();
    renderer.enabled = false;
    rigidbody = GetComponent<Rigidbody>();
    rigidbody.useGravity = false;
}
void Update()
{
  if(Time.time > timeToWait)
  {
      renderer.enabled = true;
      rigidbody.useGravity = true;
  }
}

}

Thank you. From what I can see, the Dropper class does not contain any code which changes the colour of the game object. That’s why the dropper does not change its colour when it hits the ground. At least, the Dropper component does not change the colour.

Which component is supposed to change the colour of the dropper?

The Mesh Renderer right?
I want the object to stay invisible until it falls down and after it touches the ground it should have the same color as the other obstacles

Haha, yes, you are right. I was unprecise. I meant: Which component overrides the material.color property in your code?

So should I make an if statement now that says if my object touches the ground enable the mesh renderer and remove the renderer statement from the Time.time if statement

First of all, your code needs to register the collision. However, methods don’t get called automatically. Fortunately, Unity provides a method which gets called when a collision happens: OnCollisionEnter. In that method, you could write the code for changing the colour. An if-statement is necessary only if you want to ensure that only specific game objects trigger the changing of the colour. Give it a try. :slight_smile:


By the way, this is Rick’s final code for the ObjectHit class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectHit : MonoBehaviour
{
    private void OnCollisionEnter(Collision other) 
    {
        if(other.gameObject.tag == "Player")
        {
            GetComponent<MeshRenderer>().material.color = Color.red;
            gameObject.tag = "Hit";
        }
    }
}

As you can see, the relevant code is wrapped in an if-statement. Since the ground does not have the “Player” tag, the if-block does not get executed when the Dropper hits the ground. Before you write your own code, remove the if-statement from the ObjectHit class. Maybe that’ll solve the problem for you.