Wallpaper Engine

Wallpaper Engine

Not enough ratings
Furry Girl Lite
   
Award
Favorite
Favorited
Unfavorite
Age Rating: Everyone
Genre: Animal
Resolution: Other resolution
Category: Wallpaper
File Size
Posted
35.773 MB
Oct 31, 2016 @ 8:01pm
1 Change Note ( view )

Subscribe to download
Furry Girl Lite

Description
Simple character looking at the mouse cursor algorithm.

Here is the code in C#
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LookAtMouse : MonoBehaviour
{

// speed is the rate at which the object will rotate

public float speed = 10f;

Plane ground;

public bool GameFocus;
public float timeStamp;
public float coolDownPeriodInSeconds;
Quaternion randomTarget;

void Awake()
{
// it is kinda important to make this app run in background, for the random target work.
Application.runInBackground = true;
}

void Update()
{
RotateToMousePosition();
}

void RotateToMousePosition()
{
ground = new Plane(Vector3.back , transform.position);

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

//*distance only needs an initial value to work
float dist = 0f;

if (ground.Raycast(ray, out dist))
{
//create a vector position at latest raycast hit point
Vector3 mousePoint = new Vector3(ray.GetPoint(dist).x,
ray.GetPoint(dist).y, (ray.GetPoint(dist).z - 2));

//*useful for showing where ray comes from in testing ONLY
Debug.DrawLine(ray.origin, mousePoint);

//rotate object toward vector position
Quaternion targetRotation = Quaternion.LookRotation(mousePoint - transform.position);
//IF Game has focus, it will look to targetRotation, else, will randonly generate a location
if (GameFocus)
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
else
{
if (timeStamp <= Time.time)
{
randomTarget = Quaternion.LookRotation(new Vector3(Random.Range(-5, 5), Random.Range(-5, 5), -4.9f));
timeStamp = Time.time + coolDownPeriodInSeconds;
}
transform.rotation = Quaternion.Slerp(transform.rotation, randomTarget, speed * Time.deltaTime);
}
}
}

// this is the function to check if the app has focus
void OnApplicationFocus(bool hasFocus)
{
GameFocus = hasFocus;
}
}
< >
1 Comments
Luckyday Feb 17, 2017 @ 2:40am 
very good