FooFily
October 11, 2021, 12:41pm
1
I am trying to register collisions between 2 objects.
I am trying to debug.log when they do collide, but it doesn’t work.
Player Settings: Screenshot - a6d469ba0d7495319f4a9942d2fdbca8 - Gyazo
Player Test Settings: Screenshot - 4274d7dc0747778b8e7832dd5203906e - Gyazo
They don’t collide: Screen capture - 48bbb39c63237d0f30edf4a0e926d41f - Gyazo
Code of Player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
float moveSpeed = 2f;
[SerializeField]
GameObject hitShape;
float x;
float y;
SpriteRenderer sprite;
void Start()
{
transform.position = new Vector2(0, 0);
sprite = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
Movement();
if (Input.GetMouseButtonDown(0))
{
Punch();
}
}
private void Movement()
{
if ((Input.GetAxisRaw("Horizontal") != 0) && (Input.GetAxisRaw("Vertical") != 0))
{
transform.Translate(new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * Time.deltaTime * moveSpeed * 0.75f);
}
else
transform.Translate(new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * Time.deltaTime * moveSpeed);
}
private void OnCollisionEnter2D(Collision2D other)
{
Debug.Log("hee");
}
}
Help please.
With Physics (or Physics2D), never move colliders by the transform directly. You are bypassing the physics system.
Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) to move things.
Kurt-Dekker:
With Physics (or Physics2D), never move colliders by the transform directly. You are bypassing the physics system.
Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) to move things.
Hey! Thank you for your reply.
I modified my code to move the Rigidbody instead of the transform itself, but collision still doesn’t work.
Code"
private void Movement()
{
m_Input = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0);
if ((Input.GetAxisRaw("Horizontal") != 0) && (Input.GetAxisRaw("Vertical") != 0))
{
m_rigidbody.MovePosition(transform.position + m_Input * Time.deltaTime * moveSpeed * 0.75f);
}
else
m_rigidbody.MovePosition(transform.position + m_Input * Time.deltaTime * moveSpeed);
}
It’s only doing what you asked it to do which is be a trigger because you’ve specified the “player” to be a trigger meaning you don’t want a collision. So maybe decide what you want i.e. trigger or not a trigger.
I want a trigger collision. I want to be able to go through the other object but take action in code as a result of that collision, but it doesn’t detect it at all.
Solution: Use “OnTriggerEnter2D” instead of “OnCollisionEnter2D”.
Note that the thread is saying “Collision” won’t register. So you don’t want a collision but you are asking for it to register but you actually want a trigger. You also seemed to indicate that you wanted it to actually collide. I would suggest being clearer in the future on what you want because I would’ve suggested the above if I knew that you wanted a trigger.