- Home /
How do you create crosshairs that are controlled by the cursor/mouse similar to AngryBots?
Firstly, I am completely new to game design and I'm not very savvy with Javascript or any other programming utility. I have basically been just copy and pasting everything I've found so far, which is fine. But I would really like to develop a custom crosshair/target script that behaves like the player's cursor in AngryBots, or also like Super Mario Galaxy 2's Yoshi tongue. In other words, the mouse becomes the cursor embedded into the game, and acts as the firing trajectory's target. I'm planning on making a game that involves the use of a tongue like Yoshi's, where you aim at a point on the screen, press the fire button, and a tongue shoots out then snaps back in. If someone could please point me in the right direction that would be great. Thanks very much everyone!
Answer by Unity_Mike2012 · Aug 13, 2012 at 04:37 AM
drak0,
Thank you for the feedback, I'll go through the tutorial. My concern though is that I might not find exactly what I am looking for, and spend a lot of time learning things that I don't necessarily need to learn right away because this is an FPS tutorial. Shooter games aren't what I'm interested in developing. The game I would like to create, was envisioned as third person, so I've been trying to find something in regards to a Zelda-oriented gamer interface. In other words, the action would be third person based, and utilizes at least two different cameras with two different scripts, one for the character, and one for attack targeting, similar to that of Zelda. However, I am also not sure how to really model the actor of the game because the more I play around with Unity the more I am finding that the tongue of the actor (frog) would have to be modeled separately, and act as a child object to the character object. Again, I'm very new to the fundamentals of Unity, Modeling and Rigging, and if there is anything out there that someone knows about regarding third person cameras like Zelda, that would be great. I'm hoping to make this game, and then a blog to host the game for the public to play. Anyone that can give me some tips will be credited in the development of the game. Thanks again, drak0 I really appreciate your advice. Thanks everyone!
Answer by drak0 · Aug 13, 2012 at 05:10 AM
Ok, I understand, but some of the things explained there are not only for FPS games, even more, when you understand how they work together, you can adapt them for your needs.For 3rd person, try this .
I have a question regarding something I came across in the FPS tutorial. When I made the rocket in Part 2 of the tutorial, I "eye-ball" aligned it to be roughly close to concentric with the diameter of the barrel it should launch from. However, when playing the game I noticed the rocket does not fire from the transform position I aligned it to. So I looked at the script for the rocket launcher called "Rocket Launcher.js" and this is what I noticed:
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position,
transform.rotation);
Now this line of code is pretty self explanatory so you probably know what my next question is: Is it possible to add an equation such as
var instantiatedProjectile : Rigidbody = Instantiate (projectile, **transform.position +2.0**,
transform.rotation);"
The question being, if I add an value, in this instance it's "transform.position + 2", shouldn't that add to the Y transform position of the instantiatedProjectile? or how exactly would I go about doing that in Javascript? And it would be nice to know how to manipulate not just the Y coordinate, but the X and Z coordinates as well. I apologize for my lack of comprehension of the script, although I am learning slowly from the tutorial what each function does, it's a lot to take in though I'll admit. Please let me know if you can, I think having the ability to change that would make the launch look "visually correct". Thanks for the help!
Answer by drak0 · Aug 13, 2012 at 07:55 PM
You can't really do that, position is of type Vector3.That is why you should use the documentation ( you have the link in my first answer). Read this, all of it.So if you would want to alter Y , your code should be something like this :
Vector3(transform.position.x, transform.position.y + N, transform.position.z)
instead of the "transform.position + N ".But, as a better way, you could create an empty GameObject as a spawning point.Then , make the barrel of the gun a parent for the spawning point, and after that, tweak the position of the spawning point visually.Expose a variabile in the script to refer to the spawning point, so it will appear in the inspector and you just drag and drop it there.And after that, in the script, when you instantiate , you use the position of the spawning point.You will understand why the whole parent thing, if you read the link from this answer.
Ok I got the rocket re-alignment figured out with your advice of making the spawn point an empty game object where the barrel of the launcher is, but now I've been trying to find out how to make the bullets of the machinegun play a sound when they hit something. I tried copying the audio code so that it basically looks like this
function FireOneShot () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit;
// Did we hit anything? if (Physics.Raycast (transform.position, direction, hit, range)) { // Apply a force to the rigidbody we hit if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface! // And spawn a couple of particles if (hitParticles) { hitParticles.transform.position = hit.point; hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); hitParticles.Emit();
if (audio) { if (!audio.isPlaying) audio.Play(); audio.loop = true; } } else {
Sparks.enabled = false; enabled = false;
// Play sound if (audio) { audio.loop = false; }
Of course I know this is wrong, but am I on the right track at least? I just can't figure out how to link the hitParticles to a sound. Please let me know when you get a chance, I tried looking up all the material you shared with me and I just can't seem to figure it out, because again, I don't know when to use or when not to use all of the different Javascript functions. Thanks again!
Answer by drak0 · Aug 14, 2012 at 01:01 PM
Well, if the machinegun was shooting bullets as instantiated projectiles, I would check the collisions in a script attached to the projectile prefab(that being the audio source).Since it is not the case, why don't you try to just play the audio at the location of the hit? I can't say much about the performance of this way of doing things, but... you get the position of the hit where you check if you hit something. So if you hit something, play a hit sound at that location.Use this .
Also, when you copy paste code here, do it like this " <pre><code> Insert code here </code ></pre> " . This will make it readable.
Your answer
Follow this Question
Related Questions
Mouse Focus and Gui.Window 0 Answers
Mouse move to control the animation 1 Answer
How to get mouse cursor in Vector3 1 Answer
How to convert Touch Input To Mouse Input C# unity? 0 Answers
How to interact with world space UI with a crosshair? 1 Answer