Skip to content

Instantly share code, notes, and snippets.

@Bradshaw
Created November 12, 2018 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bradshaw/95a93d2272e9c6987663dd3c8dc0e6c7 to your computer and use it in GitHub Desktop.
Save Bradshaw/95a93d2272e9c6987663dd3c8dc0e6c7 to your computer and use it in GitHub Desktop.
BG1-A
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Walkabout : MonoBehaviour {
public float acceleration = 1;
public float friction;
public float radius;
Vector3 velocity;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if ( Input.GetKey( KeyCode.RightArrow ) ){
velocity += Vector3.right * acceleration * Time.deltaTime;
}
if ( Input.GetKey( KeyCode.LeftArrow ) ){
velocity += Vector3.left * acceleration * Time.deltaTime;
}
if ( Input.GetKey( KeyCode.UpArrow ) ){
velocity += Vector3.up * acceleration * Time.deltaTime;
}
if ( Input.GetKey( KeyCode.DownArrow ) ){
velocity += Vector3.down * acceleration * Time.deltaTime;
}
velocity -= velocity * friction * Time.deltaTime;
RaycastHit2D result = Physics2D.CircleCast(transform.position, radius, velocity.normalized);
if (result.collider != null){
if (Vector3.Distance(transform.position, result.point)<radius+velocity.magnitude*Time.deltaTime){
velocity = Vector3.zero;
}
}
transform.position += velocity * Time.deltaTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment