- Home /
Question by
deadlycoconut7 · Dec 27, 2017 at 01:32 AM ·
c#script.
A few code suggestions
I am making a short game about a Runaway Block where it tracks how far you made it each level. Buuuut, there's a few things I am trying to add to it that I cannot figure out. I am trying to add code to stop the distance tracker once the cube hits an object. Also, trying to add a feature for once you hit a certain checkpoint, It will revert the players direction without losing speed. So I can keep the game a little more neat and contained. This might be a bit long because this is my first time asking for help on the unity community thing, but if anyone has any ideas, I'll be very grateful. Thank you and much love.
public class Playermovement : MonoBehaviour {
//This is a reference to the rigidbody component
public Rigidbody rb;
//Sets the force of the object
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
void Start () {
}
// Unity likes fixed update for physics
void FixedUpdate () {
//Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
//Adds force to the x axis for control
if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
} public class Score : MonoBehaviour {
public Transform player;
public Text scoreText;
// Update is called once per frame
void Update () {
scoreText.text = player.position.z.ToString(".00");
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
public class Camera : MonoBehaviour {
public Transform player;
public Vector3 offset;
// Update is called once per frame
void Update () {
transform.position = player.position + offset;
}
} public class PlayerCollison : MonoBehaviour {
public Playermovement movement; // Reference to Player Movement script
// This function runs when we hit another object
//We get info about the collsion and call it "collsionInfo"
void OnCollisionEnter(Collision collisionInfo)
{
//We check if the object we collided with has tag "Obstacle"
if(collisionInfo.collider.tag == "Obstacle")
{
movement.enabled = false; // Disables the characters movement
FindObjectOfType<GameManager>().EndGame();
}
}
}
Comment