Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class playerController : MonoBehaviour
- {
- public float horizontalInput;
- public float verticalInput;
- public float speed = 10.0f;
- //range for which player can move left and right
- public float xRange = 10;
- //range for which player can move froward and back
- public float zRange = 10;
- //slot for food
- public GameObject projectilePrefab;
- public Transform projectileSpawnPoint;
- void Start()
- {
- }
- void Update()
- {
- //keep player inbounds on the left side
- if (transform.position.x < -xRange)
- {
- transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
- }
- //keep player inbounds on the right side
- if (transform.position.x > xRange)
- {
- transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
- }
- //move the player left and right
- horizontalInput = Input.GetAxis("Horizontal");
- transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed);
- //move the player forward and back
- verticalInput = Input.GetAxis("Vertical");
- transform.Translate(Vector3.forward * verticalInput * Time.deltaTime * speed);
- //keep player inbounds on the lower side
- if (transform.position.z < -zRange)
- {
- transform.position = new Vector3(transform.position.x, transform.position.y, -zRange);
- }
- //keep player inbounds on the upper side
- if (transform.position.z > zRange)
- {
- transform.position = new Vector3(transform.position.x, transform.position.y, zRange);
- }
- //launch projectile using spacebar
- if (Input.GetKeyDown(KeyCode.Space))
- {
- Instantiate(projectilePrefab, projectileSpawnPoint.position, projectilePrefab.transform.rotation); ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement