using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody playerRb;
private Animator playerAnim;
public float jumpForce = 10;
public float gravityModifier;
public bool isOnGround = true;
public bool gameOver = false;
// Start is called before the first frame update
void Start()
{
playerRb = GetComponent<Rigidbody>();
playerAnim = GetComponent<Animator>();
Physics.gravity *= gravityModifier;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
{
// The Traing Video Code shows this line with a "10" before jumpForce
// whenever I try and add the "10" i get compile errors
playerRb.AddForce(Vector3.up * 10 * jumpForce, ForceMode.Impulse);
isOnGround = false;
playerAnim.SetTrigger("Jump_trig");
{
}
}
}
// here is the first new reference to "other" this is where I am not sure how
// to fix this since it appears that the reference is invalid how do I fix this?
private void OnCollisionEnter(Collision collision other)
{
if (other.gameObject.CompareTag("Ground"))
{
isOnGround = true;
} else if (other.gameObject.CompareTag("Obstacle"))
{
Debug.Log("Game Over Woody");
gameOver = true;
playerAnim.SetBool("Death_b", true);
playerAnim.SetInteger("DeathType_int", 1);
}
}
}
I am attempting to complete the Tutorial for “Create with Code” Lesson 3.3 Don’t Just Stand There. Step 6 Set up a Falling Animation I’m having problems with the code where it is referencing “other” but it appears it was never defined so I’m getting compile errors. I’m new to all of this but determined to learn.
I am also having problems with the reference to the variable jumpFoce it shows in the video example the playerRb.AddForce(Vector3.up * 10 jumpForce, ForceMode.Impulse); but this creates errors as well so I removed the “10” and the errors went away is this correct?
Here is my code for the PlayerController.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { private Rigidbody playerRb; private Animator playerAnim; public float jumpForce = 10; public float gravityModifier; public bool isOnGround = true; public bool gameOver = false;
Apologies you ran into this issue! These errors were indeed caused by the “other” contained within the OnCollisionEnter function. This was included in the code screenshot by mistake - simply removing “other” from the code will fix your errors!
Within the function we’re checking for a Collision that we’re naming “collision” - “other” is unnecessary as it doesn’t do anything in this context, thus confusing the compiler and causing errors.
I’ve updated the code screenshot in the tutorial now so it won’t be an issue in the future - thanks for reporting this to us!
Good luck with the rest of the course