WHY ISN'T THIS WORKING?

I apologize for the CAPS, but I am really frustrated. I have been at this for 3 hours and still haven’t been able to get it to work. I’m on the 5th tutorial for the Space Shooter series. I don’t have any background in coding, so I would really appreciate some help. Here it is:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed;
public float xMin, xMax, zMin, ZMax;

void Start () {
rb = GetComponent ();

}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);

Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;

rb.position = new Vector3
(
Mathf.Clamp (rb = GetComponent (rb.position.x, xMin, xMax)),
0.0f,
Mathf.Clamp (rb = GetComponent (rb.position.z, zMin , zMax))
);
}
}

Use code tags:

What is your question specifically? Aside from “trying to get it to work”.

How about link to the tutorial you’re following, and the part of it you’re not understanding, and what you expect to occur.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed;
public float xMin, xMax, zMin, ZMax;

void Start () {
rb = GetComponent<Rigidbody> ();

}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");

Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;

rb.position = new Vector3
(
Mathf.Clamp (rb = GetComponent<Rigidbody> (rb.position.x, xMin, xMax)),
0.0f,
Mathf.Clamp (rb = GetComponent<Rigidbody> (rb.position.z, zMin , zMax))
);   
}
}

Tutorial Link: https://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player?playlist=17147

I’m stuck around 13:00. I can’t seem to get the x and z values to appear under the script tab in the Inspector Window

Okay, it’s because you probably have errors in your code.
First, you have a small typo with ‘ZMax’ instead of ‘zMax’ at the top.

Next, what you want to do is (change this section):

rb.position = new Vector3
(
Mathf.Clamp (rb.position.x, xMin, xMax),
0.0f,
Mathf.Clamp (rb.position.z, zMin , zMax)
);

Just a few moments after where you said you’re stuck in the tutorial, the presenter splits the min/max variables into its own class, if you follow along. Hope that helps :slight_smile:

1 Like

Now it says that the associated script cannot be loaded

Never mind. It’s all set. Thank you so much for your help.

1 Like

Cool, glad ya got it working :slight_smile:

1 Like