简体   繁体   中英

I have no syntax errors in Unity, How can I find errors so I learn why my crosshair stays in the center of the screen?

For Unity Game: shouldn't the cross-hair move accordingly as the player rig does to point at enemies? how can I find error with no syntax error messages?

I tried visiting Unity forums I found support about raycasting followed directions add some code to the crosshair.cs script I receive no syntax errors.

public class CrossHair: MonoBehaviour{

[SerializeField] private GameObject standardCross;
[SerializeField] private GameObject redCross;

float moveForce = 1.0f;
float rotateTorque = 1.0f;
float hoverHeight = 4.0f;
float hoverForce = 5.0f;
float hoverDamp = 0.5f;

Rigidbody rb;
private RaycastHit raycastHit;
void Start()

{

    standardCross.gameObject.SetActive(true);
    rb = GetComponent<Rigidbody>();

    // Fairly high drag makes the object easier to control.
    rb.drag = 0.5f;
    rb.angularDrag = 0.5f;
}

void Update()

{
// Push/turn the object based on arrow key input.
rb.AddForce(Input.GetAxis("Vertical") * moveForce * transform.forward);
rb.AddTorque(Input.GetAxis("Horizontal") * rotateTorque * Vector3.up);

    RaycastHit hit;
    Ray downRay = new Ray(transform.position, -Vector3.up)
    if (Physics.Raycast(downRay, out hit))
    {
 //The "error" in height is the difference between the desired height
        // and the height measured by the raycast distance.
        float hoverError = hoverHeight - hit.distance;

       // Only apply a lifting force if the object is too low (ie, let
        // gravity pull it downward if it is too high).
        if (hoverError > 0)
        {
        // Subtract the damping from the lifting force and apply it to
        // the rigidbody.
      float upwardSpeed = rb.velocity.y;
      float lift = hoverError * hoverForce - upwardSpeed * hoverDamp;
      rb.AddForce(lift * Vector3.up);
     }
}
Ray targettingRay = new Ray(transform.position, transform.forward)

    if (Physics.Raycast(targettingRay, out raycastHit, 100))
    {
        if (raycastHit.transform.tag == "Enemies")

        {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            redCross.gameObject.SetActive(true);
            standardCross.gameObject.SetActive(false);
        }
    }

    else

    {
        redCross.gameObject.SetActive(false);
        standardCross.gameObject.SetActive(true);
    }
}
}

I except for the crosshair in my game to follow the player rig camera as the code explains. Any guidance is appreciated.

Syntax errors are those kind of errors which prevent your game/program from running at all. They are like grammar mistakes that need to be fixed before you can run your code.

A syntax error is your pc telling you:

I can't run this because I don't understand this. or I can't run this because this is not allowed.

One example would be int x = "Hello World" . This in not allowed because I can not assign an string value to an integer (like that).

But your code not having any syntax errors does not mean it will do what you intended it for , it just means your code will run.

A good and easy way of debugging your code in Unity is to add Debug.Log("My Log Message"); statements to your code where you think it would be beneficial. These are going to be logged to your console output in Unity while you are in play mode. You can for example do something like this to constantly get your cross's position and rotation logged:

Debug.Log("Cross Position: " + standardCross.transform.position);
Debug.Log("Cross Rotation: " + standardCross.transform.rotation);

Just be sure to remove them once you are done with them because having Debug.Logs in your code takes as significant hit to your performance.

Another, more sophisticated way of debugging your code is through the usage of breakpoints in Visual Studio or whatever IDE/Editor you are using. It basically comes down to declaring points where your program should pause execution for you to look at values in your program itself. It's quite handy but goes above and beyond what I could tell you via this text, so please have a look at this Unity-specific use case: Debugging Unity games with Visual Studio

Now to your code:

First: There is a Vector3.down so you don't have to use -Vector3.up.

Second: Do you need a GameObject as crosshair? Why not just add a UI-Crosshair instead? That way it always stays in the middle of your screen wherever you turn your camera.

Just add a new Image to your UI via GameObject -> UI -> Image, give it some kind of crosshair look in the inspector and lock it to the middle of the screen by left-clicking on the little crosshair in the top left of the Inspector while you have your Image selected and then Shift + Alt + Left click the middle option.

If you really want to use a separate gameObject as crosshair then maybe attatch it to your player object as a child. That way it will move and rotate with your player automatically and you do not have to do this via script.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM