2D Collisions Not Working,2d Collision not working?
Hi, I'm making a very basic unity game in 2D just for testing. However, I've been trying for several days and I can't get the colliders to work at all. I'm trying to make a basic object, a tree, something the player can't pass through. However, even though I have rigidbodies and colliders on both objects, I can't get it to work unless I set the tree to static and the player to dynamic (which ruins the control scheme I have on it, since after colliding with an object it will drift in one direction without ever stopping). I've searched up tutorials but nothing has been helpful all. Any help would be appreciated.
The code on the player for movement is this, if that would help: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody2D rb2d;
private Vector2 moveVelocity;
private bool facingRight;
void Start () {
facingRight = true;
rb2d = GetComponent<Rigidbody2D>();
}
void Update () {
Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
moveVelocity = moveInput.normalized * speed;
}
private void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
Flip(horizontal);
rb2d.MovePosition(rb2d.position + moveVelocity * Time.fixedDeltaTime);
}
private void Flip(float horizontal)
{ if (horizontal > 0 && !facingRight || horizontal<0 &&facingRight)
{ facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}` ,Hi, I'm making a very basic unity game in 2D just for testing. However, I've been trying for several days and I can't get the colliders to work at all. I'm trying to make a basic object, a tree, something the player can't pass through. However, even though I have rigidbodies and colliders on both objects, I can't get it to work unless I set the tree to static and the player to dynamic (which ruins the control scheme I have on it, since after colliding with an object it will drift in one direction without ever stopping). I've searched up tutorials but nothing has been helpful all. Any help would be appreciated.
The code on the player for movement is this, if that would help: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody2D rb2d;
private Vector2 moveVelocity;
private bool facingRight;
void Start () {
facingRight = true;
rb2d = GetComponent<Rigidbody2D>();
}
void Update () {
Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
moveVelocity = moveInput.normalized * speed;
}
private void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
Flip(horizontal);
rb2d.MovePosition(rb2d.position + moveVelocity * Time.fixedDeltaTime);
}
private void Flip(float horizontal)
{ if (horizontal > 0 && !facingRight || horizontal<0 &&facingRight)
{ facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}
Your answer
Follow this Question
Related Questions
Collision check 0 Answers
Collision With Text 0 Answers
Absolutely no collision detection in build but editor is fine 1 Answer
Collider Issue 0 Answers
my colliders don't work properly 2 Answers