- Home /
Questions about Basic Platformer Live training script
So I'm trying to learn coding and game development, and I've been taking advantage of all the tutorials set up by Unity. I recently just finished the Basic Platformer Live Training tutorial and I understand what's going on fairly well but I have a few questions that I wasn't able to figure out myself.
So below I have a copy of the SimplePlatformController script from the tutorial with a whole bunch of comments from me trying to figure some things out. I was able to figure a few things out myself, and I'll leave the comments there just in case I was wrong about any of them. But I'll also put my unanswered questions at the bottom in bold.
public class SimplePlatformController : MonoBehaviour
{
[HideInInspector] public bool facingRight = true;
[HideInInspector] public bool jump = false;
public float moveForce = 365;
public float maxSpeed = 5;
public float jumpForce = 1000;
public Transform groundCheck;
private bool grounded = false;
private Animator anim;
private Rigidbody2D rb2d;
void Awake()
{
anim = GetComponent <Animator> ();
rb2d = GetComponent <Rigidbody2D> ();
}
void Update()
{
grounded = Physics2D.Linecast (transform.position,
groundCheck.position,
1 << LayerMask.NameToLayer("Ground"));
/* What's happening here is kind of confusing. The bitwise left-
* shift or '<<' moves the bits of information determined by the
* lefts side of the operator, or in this case 1 (00000001) to
* the left by an amountdetermined by the right side of the
* operator or in this case`LayerMask.NameToLayer("Ground").
*
* The function LayerMask.NameToLayer looks for a layer that
* matches thestring parameter field and returns the index int
* value of that layer.
*
* Assuming that "Ground" is the 10th layer
* 00000001 will change to 00000001
* (being moved 9 bits to the left).
*/
/* Does the LineCast function need a bit value
* for the Layer perameter or was the bitwise
* left-shift unnecessary?
*
* But isn't that a completely different layer than the
* ground anyway? Shouldn't the "Ground" layer be 00000101?
*/
if (Input.GetButtonDown ("Jump") && grounded)
{
jump = true;
}
}
void FixedUpdate()
{
float h = Input.GetAxis ("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(h));
if (h * rb2d.velocity.x < maxSpeed)
/* If we're using `maxSpeed` to clamp the velocity of the player
* then why can't we just say if `rb2d.velocity.x < maxSpeed`
* instead of multiplying that by `h`?
*/
rb2d.AddForce(Vector2.right * moveForce * h);
// how will the rigidbody2d velocity decrease?
if (Mathf.Abs (rb2d.velocity.x) > maxSpeed)
rb2d.velocity = new Vector2
(Mathf.Sign(rb2d.velocity.x) *
maxSpeed, rb2d.velocity.y);
if (h > 0 && !facingRight)
Flip ();
else if (h < 0 && facingRight)
Flip ();
if (jump)
{
anim.SetTrigger("Jump");
rb2d.AddForce (new Vector2 (0f, jumpForce));
jump = false;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
// Why does this need to be declared a second time?
}
}
Why is the bitwise left shift needed in the LayerMask parameter of LineCast?
If "Ground" is the 10th layer in the index (having an index value of 9), won't the bitwise left shift result in a completely different number?
Why is the rigidbody's velocity multiplied by the player input when checking to see if it's less than the maxSpeed value in order to move the player?
If AddForce is being used to move the player shouldn't it take a longer time for the player's velocity to decrease?
if Vector3 theScale = transform.localScale;
was already declared to set theScale
to equal the local scale, why does it need to be declared again after theScale
is multiplied by -1?
Answer by CorruptedTNC · Apr 24, 2015 at 12:30 PM
Ok so the bitwise operator in this case indicates that only the layer labelled as "Ground" should be hit by the LineCast. This is a handy way to ignore various objects if you don't want something to collide with certain objects.
There's some information about what it actually represents in this forum post from 2012
By using LayerMask.NameToLayer("Ground") you're guaranteed that it will find the layer labelled as Ground regardless of which layer number it has. This is far more reliable than giving it a number, since settings like layers and tags can change frequently (especially in the early stages of a project).
When checking for the speed being less than max speed, it's mostly because that's how the calculations are written for the character's speed. The check needs to be put in to place before the force is added to determine whether or not it will end up going over the max speed. If these sort of questions come up, try removing a portion of it (in this case multiplying by h) and see what happens. There are usually some pretty interesting results
Rigidbodies take in to account mass, friction, drag etc, so they handle their own speed decreasing. In most cases you won't have to specify that you want the rigidbody to slow down.
theScale isn't being declared twice, have another read over it. Basically to change the way the object is facing, the scale on the x axis is multiplied by -1. So a copy of transform.localScale is stored and then modified, after which transform.localScale is set to the new Vector3. It's not being declared twice, rather you're getting the localScale, modifying it and then setting it to the new value.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
One small step ... 1 Answer