- Home /
Hi everone! I am new to Unity so sorry if this question stupid. But basically can you help me fix the Code?
For now, it will debug to the log Not Hit every time the objects collide instead of debugging hit when its less than zero. Can you please help me fix the code? Thanks!! This is the code: using UnityEngine; using System.Collections; public class PlayerMovement : MonoBehaviour {
public float moveSpeed = 05f;
public VJHandler jsMovement;
private Vector3 direction;
private float xMin, xMax, yMin, yMax;
public GameObject BlueX;
void Start()
{
//Initialization of boundaries, used to be 50
xMax = Screen.width - 15;
xMin = 15;
yMax = Screen.height - 15;
yMin = 15;
}
void Update()
{
direction = jsMovement.InputDirection; //InputDirection can be used as per the need of your project
if (direction.magnitude != 0)
{
transform.position += direction * moveSpeed;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, xMin, xMax), Mathf.Clamp(transform.position.y, yMin, yMax), 0f);//to restric movement of player
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "BLUEPLAYER")
{
if (BlueX.transform.position.x <= 0)
{
Debug.Log("HIT");
//return;
}
else if (BlueX.transform.position.x >= 0)
{
Debug.Log("Not Hit");
}
}
}
}
Well, code doesn't lie. If "Not hit" is being debugged, then BlueX object's X position is always >= 0. So you need to figure out why that is.
Answer by darkStar27 · Dec 07, 2018 at 04:25 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Boundary {
public float xMin, xMax, yMin, yMax;
}
public class PlayerMovement : MonoBehaviour {
public float moveSpeed = 05f;
public VJHandler jsMovement;
public GameObject blueX;
private Vector3 direction;
private Boundary boundary;
void Start () {
//Initialization of boundaries, used to be 50
boundary.xMax = Screen.width - 15;
boundary.xMin = 15;
boundary.yMax = Screen.height - 15;
boundary.yMin = 15;
}
void Update () {
direction = jsMovement.InputDirection; //InputDirection can be used as per the need of your project
if (direction.magnitude != 0)
{
transform.position += direction * moveSpeed;
transform.position = new Vector3(
Mathf.Clamp(transform.position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp(transform.position.y, boundary.yMin, boundary.yMax),
0f
); //to restric movement of player
}
}
private void OnTriggerEnter2D (Collider2D collision) {
if (collision.tag == "BLUEPLAYER")
{
if (blueX.transform.position.x <= 0)
{
Debug.Log("HIT");
//return;
}
else if (blueX.transform.position.x >= 0)
{
Debug.Log("Not Hit");
}
}
}
}
I've rewritten a code above with some modifications.
First of all you can use a separate class for your boundary, this way the code will be reusable and you can also refer to the class to set boundaries to another GameObjects say you enemies.
Now from above code, it does not have any errors, but from the logic I see that you use OnTriggerEnter2D on the player. What i don't understand where are you phasing the problem.
Please explain where exactly are the errors, hat the errors are, what behavior do you expect that is not happening.
@darkStar27 Thank you for helping with the code. So there aren't errors per se, it's just that it will debug to the log not hit whether or not it's below or above zero. What I want it to do is debug to the log Hit if its X-axis position is less than zero and Not Hit if its position is more than zero. Right now it will say Not Hit either way. So I need help with that.
You can try checking for the conditions in the update function like this:
void Update()
{
if (blueX.transform.position.x <= 0)
{
Debug.Log("HIT");
//return;
}
else if (blueX.transform.position.x >= 0)
{
Debug.Log("Not Hit");
}
}
If it works then there is some problem with OnTrigger function.
@darkStar27 Thanks for the help but I actually got the answer myself. Basically, I debugged to the log the position of the GameObject and turns out the object wasn't anywhere near zero. So I fixed it. Thanks anyways!