So I had recently added this roadblock into my project, and I want to make the player stop when it touches or in other words collider, with the roadblocks. I have added rigidbody and collider to both objects(player and the roadblock. But when I run the game, it turns out that my player will just go through the roadblock even if I locked the roadblock’s position on X Y Z and rotations. I just want to make the roadblock acting like a wall which prevents player from walking/running through.
I used the transform method to move my player.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMoving : MonoBehaviour
{
public float walkingspeed;
public float runningspeed;
public float crouchwalkingspeed;
public float walkingstairsspeed;
public Transform movingObject;
public Animator anime;
bool stair = false;
// Update is called once per frame
void FixedUpdate()
{
if (Input.GetKey("d"))
{
if (Input.GetKey("space"))
{
movingObject.Translate(movingObject.right * -runningspeed * Time.deltaTime);
}
if (anime.GetBool("isCrouching") == true)
{
movingObject.Translate(movingObject.right * -crouchwalkingspeed * Time.deltaTime);
}
if (stair == true)
{
movingObject.Translate(movingObject.right * -walkingstairsspeed * Time.deltaTime);
stair = false;
}
else
movingObject.Translate(movingObject.right * -walkingspeed * Time.deltaTime);
}
if (Input.GetKey("a"))
{
movingObject.Translate(movingObject.right * walkingspeed * Time.deltaTime);
}
}
public void walkingOnStairs()
{
Debug.Log("im on stairs");
stair = true;
}
}
Your help is extremely important for me.
Thank you!