Hello everyone,
I am kinda confused with the rigidbody in Unity.
I am trying to prevent players to walk through walls ( the tank in the image, and the red color is the wall )
I have added edge collider following the color…also I have added rigidbody on the player…and its not working.
any solution?
searched around and found an answer but not very sure if that’s the real solution to do it or not.
should i change my game to 3D and use 3D rigidbody? or is there another way to workaround
Does your character have a collider? Do you move the rigidbody2d properly, i.e. with physics and not manipulating the transform directly?
My character has a boxcollider…right now I am using circle collider and it works… but with box collider it doesnt work.
PS i am also trying to make my character able to shoot…so there is incomplete code
using UnityEngine;
public class Movement : MonoBehaviour
{
public Animator animator;
[SerializeField]
private float speed = 3.5f;
private KeyCode UpButton = KeyCode.W;
private KeyCode DownButton = KeyCode.S;
private KeyCode LeftButton = KeyCode.A;
private KeyCode RightButton = KeyCode.D;
private KeyCode Shoot = KeyCode.Space;
private Vector2 direction;
private Quaternion rotation = Quaternion.identity;
private Vector2 zero = Vector2.zero;
private float bulletSpeed = 1.0f;
public bool endOfShooting;
public GameObject bulletPrefab;
private Rigidbody2D rb;
// When its excuted
void Start()
{
rb.GetComponent<Rigidbody2D>();
}
// Loop
void Update()
{
getShoot();
getInput();
Move();
if (direction == zero)
{
animator.SetInteger("direction", 0);
}
}
public void Move()
{
transform.position += (Vector3)(direction * speed * Time.deltaTime);
transform.rotation = rotation;
}
//Shoot script
void getShoot()
{
//in progress
if (endOfShooting)
{
GameObject Bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
Bullet.GetComponent<Rigidbody2D>().velocity = shootingDirection * bulletSpeed;
}
}
// Keyboard movement input by player
private void getInput()
{
direction = zero;
if (Input.GetKey(UpButton))
{
if (Input.GetKey(DownButton) || Input.GetKey(LeftButton) || Input.GetKey(RightButton))
{
direction = zero;
}
else
{
direction.y += 1;
animator.SetInteger("direction", 1);
}
}
if (Input.GetKey(DownButton))
{
if (Input.GetKey(UpButton) || Input.GetKey(LeftButton) || Input.GetKey(RightButton))
{
direction = zero;
}
else
{
direction.y -= 1;
animator.SetInteger("direction", 1);
}
}
if (Input.GetKey(LeftButton))
{
if (Input.GetKey(DownButton) || Input.GetKey(UpButton) || Input.GetKey(RightButton))
{
direction = zero;
}
else
{
direction.x -= 1;
animator.SetInteger("direction", 1);
}
}
if (Input.GetKey(RightButton))
{
if (Input.GetKey(DownButton) || Input.GetKey(LeftButton) || Input.GetKey(UpButton))
{
direction = zero;
}
else
{
direction.x += 1;
animator.SetInteger("direction", 1);
}
}
//So we remain in the last rotation set when player releases keys rather than flipping back to default.
if (direction != zero)
{
rotation = Quaternion.LookRotation(Vector3.forward, direction);
}
}
}