- Home /
Question by
Aqualime404 · Jan 04 at 04:58 PM ·
raycast
How to make a mouse object highlighter and zoom in to objects
public float maxdistance = 10f;
public Transform currenthit = null;
public Vector3 originpos;
public LayerMask Clickable;
// Start is called before the first frame update
void Start()
{
originpos = transform.position;
}
// Update is called once per frame
void Update()
{
ObjectColor();
ObjectNotOn();
if(Input.GetMouseButtonDown(0)){
//Makes a ray that goes where you click, if it touches an object the camera looks at it and snaps to it
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, maxdistance, Clickable)){
if(hit.transform != null){
transform.position = originpos;
transform.rotation = Quaternion.identity;
transform.LookAt(hit.transform);
transform.Translate(transform.forward * (hit.distance - 0.2f));
transform.LookAt(hit.transform);
}
}
}
}
public void ObjectColor(){
//Creates rays automaticly that go where your mouse is, and turns the object green.
RaycastHit hitcheck;
Ray raycheck = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(raycheck, out hitcheck, maxdistance, Clickable)){
if(hitcheck.transform != null){
currenthit = hitcheck.transform;
//Add Whatever Color you want I choose green
Color mycolor = new Color(0f, 8f, 0f);
Renderer objectrenderer = hitcheck.transform.GetComponent<Renderer>();
objectrenderer.material.color = mycolor;
}
}
}
public void ObjectNotOn(){
//Does the same exept it turns the object white when your mouse isn't on it
RaycastHit hitchecknone;
Ray raychecknone = Camera.main.ScreenPointToRay(Input.mousePosition);
if(!Physics.Raycast(raychecknone, out hitchecknone, maxdistance))
if(hitchecknone.transform == null){
//This color is just the normal color of the object, my color is white
Color mycolornone = new Color(1f, 1f, 1f);
Renderer objectren = currenthit.GetComponent<Renderer>();
objectren.material.color = mycolornone;
// When you click on nothing, it will zoom out and be back to normal
if(Input.GetMouseButtonDown (0)){
transform.rotation = Quaternion.identity;
transform.position = originpos;
}
}
}
Comment
Answer by wfreeman480 · Jan 04 at 06:29 PM
Hope I can get you going in the right direction! I was actually working on outline functions in my project last night and came across this pretty cool asset! https://assetstore.unity.com/packages/tools/particles-effects/quick-outline-115488 Just insert the script to the object you want outlined. The way I coded mine was through if statements.
If(mouse over object)
{
Set outline to true
}