Created
November 12, 2018 16:58
-
-
Save Bradshaw/95a93d2272e9c6987663dd3c8dc0e6c7 to your computer and use it in GitHub Desktop.
BG1-A
This file contains 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 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