Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class WallBounce : MonoBehaviour {
- // The player transform
- [SerializeField]
- private Transform playerTransform;
- // Is the player touching a wall?
- public bool wall = false;
- // The layer that the wall is on. Used for collision
- public LayerMask wallLayer;
- // The raduis required to detect a wall collision
- public float range = 10.0f;
- // The amount of degrees to rotate when colliding with a wall
- public float collsionRotation = 180.0f;
- void Awake() {
- playerTransform = gameObject.transform;
- }
- void FixedUpdate()
- {
- // If the car is close to a wall...
- if (Physics.OverlapSphere(transform.position, range, wallLayer)[0] != null)
- {
- // ...set wall to true
- wall = true;
- }
- // If not...
- else {
- // ...set wall to false
- wall = false;
- }
- // Rotate the car 45 degrees if it is touching a wall
- if (wall) {
- playerTransform.Rotate(collsionRotation, 0, collsionRotation, Space.Self);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement