Skip to content

Instantly share code, notes, and snippets.

@paulhayes
Last active July 5, 2017 15:58
Show Gist options
  • Select an option

  • Save paulhayes/d7a0a8e3c021aa716d8590f84bb207c2 to your computer and use it in GitHub Desktop.

Select an option

Save paulhayes/d7a0a8e3c021aa716d8590f84bb207c2 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TeleportTarget : MonoBehaviour
{
public string button = "Fire1";
public Transform lookDirection;
public Transform target;
public float maxSlopeAngle = 30;
public void Update()
{
if( Input.GetButton(button) ){
Teleport();
}
}
void Teleport()
{
RaycastHit hitInfo;
bool hit = Physics.Raycast(lookDirection.position, lookDirection.forward, out hitInfo);
if( hit ){
float currentSlope = Vector3.Dot( hitInfo.normal, Vector3.up );
float maxSlope = Mathf.Cos(maxSlopeAngle*Mathf.Deg2Rad);
if( maxSlope > currentSlope ){
target.position = hitInfo.point;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment