Created
September 5, 2017 15:51
-
-
Save aggrolite/76c7b244c00bc4bedb7470fa24cfece3 to your computer and use it in GitHub Desktop.
unity tank control
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 TankControl : MonoBehaviour { | |
public float walkSpeed; | |
public float rotateSpeed; | |
private CharacterController controller; | |
// Use this for initialization | |
void Start () { | |
controller = new CharacterController(); | |
} | |
// Update is called once per frame | |
void Update() { | |
float v = Input.GetAxis("Vertical"); | |
float h = Input.GetAxis("Horizontal"); | |
// Handle walking both forward and backwards. | |
// If walking backwards, reduce speed. | |
if (v > 0) { | |
Vector3 forward = transform.TransformDirection(Vector3.forward); | |
transform.Translate(0, 0, forward.magnitude * (walkSpeed * v)); | |
} else if (v < 0) { | |
Vector3 back = transform.TransformDirection(Vector3.back); | |
transform.Translate(0, 0, back.magnitude * (walkSpeed * v) / 4); | |
} | |
// Rotate model when L/R is pressed. | |
transform.Rotate(0, h * rotateSpeed, 0); | |
//transform.Translate(0, 0, forward.magnitude * (walkSpeed * Input.GetAxis("Vertical"))); | |
//ansform.Translate(0, 0, back.magnitude * ((walkSpeed/2) * Input.GetAxis("Vertical"))); | |
//controller.SimpleMove(forward * (walkSpeed * Input.GetAxis("Vertical"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment