Skip to content

Instantly share code, notes, and snippets.

@jhorikawa
Created January 26, 2018 23:01
Show Gist options
  • Save jhorikawa/bac5d5f3e6f02233b3234c567d4ac0b8 to your computer and use it in GitHub Desktop.
Save jhorikawa/bac5d5f3e6f02233b3234c567d4ac0b8 to your computer and use it in GitHub Desktop.
Unity script for object to follow mouse position. https://youtu.be/sa8tlx9tD-w
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Follow : MonoBehaviour {
public Material characterMarkerMat;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit)){
Vector3 mousePos = hit.point;
Vector3 dir = hit.point - transform.position;
dir.y = 0;
float speed = 0.1f;
if(dir.magnitude > speed)
{
dir.Normalize();
transform.Translate(dir * speed);
}
Vector3 spherePos = transform.position;
spherePos.y = 0;
characterMarkerMat.SetVector("Vector3_337113A", spherePos );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment