- Home /
Moving Target Circle
Ok, this is going to be hard to explain without the visuals but I'l try. I started off wanting to have a GUI target picture in the middle of my screen (The target I use to aim my bullet at). Now I've decided I want something that is like the 2d circle which surrounds the cursor here:
http://www.youtube.com/watch?v=PqvjxHfU4PI
As you can see that as the cursor moves across the object, the circle rotates and moves around the object. How will I be able to have a target in my game which is like that when you look around? It would be very cool. I'm not so sure if I'm explaining it correctly. I hope you can understand and can figure out an answer.
Here's a great answer by Aldo for finding the normal of a surface using a raycast. It then feeds information to a character controller, but the main parts are there. After watching the video I am interested and can help develop Aldos code for this purpose if you get really really stuck (or need a unity javascript version) =]
http://answers.unity3d.com/questions/168097/orient-vehicle-to-ground-normal.html
Thanks that will be a very great help! How do I convert it into Javascript? This is how the gun works if you want to know what happens:
http://www.youtube.com/watch?v=1DtbFs7e2$$anonymous$$4
A stream of particles shoot out the gun which then hit a certain wall which sends a message to the wall stating it should instantiate a cube in the position of a null object.
Answer by AlucardJay · Feb 18, 2013 at 08:27 AM
Try this :
#pragma strict
public var indicatorObject : Transform;
private var currNormal : Vector3 = -Vector3.forward;
function Update()
{
var mouseRay : Ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
var rayHit : RaycastHit;
if ( Physics.Raycast( mouseRay, rayHit, 1000.0 ) )
{
// currNormal smoothly follow the terrain normal :
currNormal = Vector3.Lerp( currNormal, rayHit.normal, 4.0 * Time.deltaTime );
// calculate rotation to follow currNormal :
var grndTilt : Quaternion = Quaternion.FromToRotation( -Vector3.forward, currNormal );
// rotate indicatorObject
indicatorObject.rotation = grndTilt;
}
}
Answer by Grug16 · Feb 13, 2013 at 07:13 AM
If you know what Raycasts are, it's fairly easy. Make a Plane with a transparent texture on it. Every frame, cast a ray from the mouse's position and set the plane's position to the hit.point and its rotation to hit.normal.
If you don't know much about Raycasts, definitely find some tutorials about them, as they are invaluable in 3D games.
It is one hell of an annoying code. Can you show me a place with a code which will make doing this much easier. I've looked around and the codes I've found are either in C# or they just don't make any sense. This might be a big ask so I'm sorry.
I eventually settled with this: http://www.youtube.com/watch?v=_UjSXAmXtJ4