Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Player : MonoBehaviour { }
- void Update () {
- Vector2 playerControls;
- playerInput.x = 0f;
- playerInput.y = 0f;
- transform.localPosition = new Vector3(playerInput.x, 1f, playerInput.y);
- playerInput.x = Input.GetAxis("Horizontal");
- playerInput.y = Input.GetAxis("Vertical");
- playerInput = Vector2.ClampMagnitude(playerInput, 2f);
- Vector3 velocity = new Vector3(playerInput.x, 0f, playerInput.y);
- Vector3 displacement = velocity * Time.deltaTime;
- transform.localPosition += displacement;
- [SerializeField, Range(1f, 1000f)]
- float maxSpeed = 20f;
- Vector3 velocity =
- new Vector3(playerInput.x, 1f, playerInput.y) * maxSpeed;
- Vector3 acceleration =
- new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
- velocity += acceleration * Time.deltaTime;
- Vector3 displacement = velocity * Time.deltaTime;
- [SerializeField, Range(1f, 1000f)]
- float maxAcceleration = 20f;
- Vector3 desiredVelocity =
- new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
- float maxSpeedChange = maxAcceleration * Time.deltaTime;
- float maxSpeedChange = maxAcceleration * Time.deltaTime;
- velocity.x =
- Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
- velocity.z =
- Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);
- [SerializeField]
- Rect allowedArea = new Rect(-5f, -5f, 10f, 10f);
- Vector3 newPosition = transform.localPosition + displacement;
- transform.localPosition = newPosition;
- Vector3 newPosition = transform.localPosition + displacement;
- if (newPosition.x < allowedArea.xMin) {
- newPosition.x = allowedArea.xMin;
- velocity.x = -velocity.x;
- }
- else if (newPosition.x > allowedArea.xMax) {
- newPosition.x = allowedArea.xMax;
- velocity.x = -velocity.x;
- }
- if (newPosition.z < allowedArea.yMin) {
- newPosition.z = allowedArea.yMin;
- velocity.z = -velocity.z;
- }
- else if (newPosition.z > allowedArea.yMax) {
- newPosition.z = allowedArea.yMax;
- velocity.z = -velocity.z;
- }
- if (newPosition.x < allowedArea.xMin) {
- newPosition.x = allowedArea.xMin;
- velocity.x = -velocity.x * bounciness;
- }
- else if (newPosition.x > allowedArea.xMax) {
- newPosition.x = allowedArea.xMax;
- velocity.x = -velocity.x * bounciness;
- }
- if (newPosition.z < allowedArea.yMin) {
- newPosition.z = allowedArea.yMin;
- velocity.z = -velocity.z * bounciness;
- }
- else if (newPosition.z > allowedArea.yMax) {
- newPosition.z = allowedArea.yMax;
- velocity.z = -velocity.z * bounciness;
- }
- //This is a very old script I made a year ago in Unity3D and never used so apologies if it doesn't work, I just learnt it from an old YouTube tutorial so it may be broken!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement