Created
February 4, 2020 15:40
-
-
Save donfreiday/941cb72fdd225065bd24e72e89db8dbb to your computer and use it in GitHub Desktop.
gamedev class snippets 001
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class CameraController : MonoBehaviour | |
{ | |
public float interpSpeed = 0.5f; | |
public GameObject focus; | |
private Vector3 targetPosition; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
focus = GameObject.Find("Mario"); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
void LateUpdate() | |
{ | |
targetPosition = new Vector3(focus.transform.position.x, transform.position.y, transform.position.z); | |
transform.position = Vector3.Lerp(transform.position, targetPosition, interpSpeed); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MarioController : MonoBehaviour | |
{ | |
public float speed = 5.0f; | |
public float jumpPower = 9.0f; | |
private Animator animator; | |
private Rigidbody2D rb; | |
private Vector3 faceRight = new Vector3(0, 90, 0); | |
private Vector3 faceLeft = new Vector3(0, -90, 0); | |
// Start is called before the first frame update | |
void Start() | |
{ | |
animator = GetComponent<Animator>(); | |
rb = GetComponent<Rigidbody2D>(); | |
ScoreManager.Current().Clear(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
float move = Input.GetAxis("Horizontal"); | |
animator.SetBool("isJumping", Input.GetKeyDown(KeyCode.Space)); | |
if (Mathf.Abs(move) > 0) | |
{ | |
ScoreManager.Current().Add(10); | |
ScoreManager.UpdateUI(); | |
animator.SetBool("isWalking", true); | |
} | |
else | |
{ | |
animator.SetBool("isWalking", false); | |
} | |
} | |
void FixedUpdate() | |
{ | |
float move = Input.GetAxis("Horizontal"); | |
if (move != 0) | |
{ | |
// todo: use SpriteRenderer.Flip() here? | |
Vector3 scale = transform.localScale; | |
if (move > 0) | |
{ | |
scale.x = Mathf.Abs(scale.x); | |
transform.position += Vector3.right * speed * Time.deltaTime; | |
} | |
else if (move < 0) | |
{ | |
scale.x = Mathf.Abs(scale.x) * -1; | |
transform.position += Vector3.left * speed * Time.deltaTime; | |
} | |
transform.localScale = scale; | |
} | |
if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
rb.AddForce(Vector3.up * jumpPower, ForceMode2D.Impulse); | |
} | |
} | |
//{ | |
// float move = Input.GetAxis("Horizontal"); | |
// //rb.velocity = new Vector2(move * speed, rb.velocity.y); | |
// if (move > 0) | |
// { | |
// if (rb.transform.eulerAngles != faceLeft) | |
// rb.transform.eulerAngles = faceLeft; | |
// transform.position += Vector3.right * speed * Time.deltaTime; | |
// } | |
// else if (move < 0) | |
// { | |
// if (rb.transform.eulerAngles != faceRight) | |
// rb.transform.eulerAngles = faceRight; | |
// transform.position += Vector3.left * speed * Time.deltaTime; | |
// } | |
// if (Input.GetKeyDown(KeyCode.Space)) | |
// { | |
// rb.AddForce(Vector3.up * 10, ForceMode2D.Impulse); | |
// } | |
//} | |
} | |
// Reference: http://coffeebreakcodes.com/sample-projects-unity/2d-mario-game-unity3d/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment