- Home /
Stop a enemy by pointing a light at it.
I want to be enabled to stop a monster like speed to = 0 by holding my RMB Pointing at him.
I´m sorry i found my own way of doing this :D
But you helped me somehow, I took a few lines from each code, So Thanks!
If you are wondering what game i´m making, http://holan.ucoz.com check this out!
Okay. I will come back later to answer this but I would like to know if you have a raycast from your player (particularly casting in the direction of your flashlight/spotlight), if so, then use the Unity referenced Physics.RayCast to cast out an effective line that can be broken by another object intersecting it, this would set you up to find an object with the ray and then do something (like, telling the object to stop moving when the ray is pointing at them ). Im around coding for a bit, so get back and let me know how you have already approached this. $$anonymous$$ainly, you should at least read this first, perhaps even copy the script below (found on reference website) and try it out in a Unity scene.
Link to reference website: link text Unity Reference Code:
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 10)) {
print ("There is something in front of the object!");
//do something here... like create a hit variable and use
//"hit.gameobject.tag" to access it and control the component
// moving the object towards the player
}
}
Okay, well done for sorting out your own problem, that`s the way to do it dude ! As I have given you basically an entire script that will do exactly as you ask succinctly, could you please mark it as your answer as it may help others who come across this issue in the future who may either be using jS or C# (to accomplish what youve asked and had answered by myself etc) Cheers bud. Gruffy
PS. I also take it you will not be need ing a raycast equivalent to show you that method of approach against this one :) Let me know :)
Answer by Gruffy · Nov 24, 2013 at 02:02 PM
Hey Danielman2 , here are two scripts that do the same thing, one in JS and one is C# as im not sure what you are working in but anyway....
INSTRUCTION: 1.Make a new cube object in scene 2.Attach this script to it in the inspector. 1. Drag your object that the enemy should target (i assume your player gameObject and drop it into script in inspector). 2. Be aware that I have placed a public slot for the actual transform you are moving, this is not necessary as you are already addressing it as script would be attached to it, but it atleast helps you understand the specific objects you are manipulating here. (change the public part to read "private" or just add the object to script in inspector, no harm will come from it) 3. Press play.
WHAT SHOULD HAPPEN NOW?...... you should see the transform moving towards your player (if placed in sight of player view, it will then move towards you slowly...using your mouse, place the cursor over the object moving towards you and press and hold "left click" it should stop....release the mouse button and it should start moving)
I will return with a RayCast option too, but i liked that dumytruncs answer, because effectively he is right.
btw, move to C#, Coding canbe hard generally but C#`s not so hard to get your head around and in a few months time you can return to here and say thanks Gruffy, you were right about moving languages and that you love C# and cant think of touching Unity with that horrible loose language that has its place, but maybe not in Unity for much longer (i dont know that, im just saying) :) Hope it all helps and if you can mark my answer if it does the job dude, that would be great Gruffy
c# version
public class TranslateObjectToTargetAndMouseControl : MonoBehaviour
{
public Transform myTrans;
public Transform target;
public float speed = 1.0f;
// Use this for initialization
void Start ()
{
//cache you public gameobject/transform
myTrans = this.transform;
}
// Update is called once per frame
void Update ()
{
//interpolate from current position to the target over time
myTrans.position = Vector3.Lerp (myTrans.position, target.position, speed * Time.deltaTime);
}
void OnMouseDown()
{
speed = 0.0f;
}
}
javascript version
pragma strict
public class TranslateObjectToTargetAndMouseControl extends MonoBehaviour
{
var myTrans : Transform;
var target : Transform; //put the object you want the transfrom to move towards (this would be you player most likely)
var speed : float = 1.0f; //the speed at which your object is moving towards player
function Start()
{
//cache you public gameobject/transform
myTrans = this.transform;
}
function Update ()
{
//interpolate from current position to the target over time
myTrans.position = Vector3.Lerp (myTrans.position, target.position, speed * Time.deltaTime);
}
function OnMouseDown()
{
//when left mouse button clicked it will stop speed
speed = 0.0f;
}
function OnMouseUp()
{
//when left mouse click is lifted, speed will return to 1.0 and start moving
speed = 1.0f;
}
}
PS..I JUST TESTED THIS AND IT WORKS PERFECTLY, just in case you feel it doesnt
and... getting answers in form of "ready to implemented scripts" is always nice but i really do suggest that you watch ALL the video tutorials in the learn section. i learned everything i need from there. hell! i learned to code C# from scratch from these videos... :)
Answer by dumytrunc · Nov 24, 2013 at 12:34 AM
function OnMouseDown ()
{
speed=0;
}
Add this to the script. OnMouseDown is called when you hold click on the enemy. OnMouseUp is called when you release the click, and you can use it to change the speed back to 5, or whatever.
Hope it helps!
Answer by Danielman2 · Nov 24, 2013 at 01:06 PM
Gruffy, It would not work??
hey bud, the code above merely demonstrated that you can get feedback from your raycast its a snippet from the reference sit.. I will return in half hour with a result for you but dunytrunc has also pointed out a fairly good alternative. If you apply the On$$anonymous$$ouseDown function by adding it to a script, and attaching it to your enemy object.....when you mouse click on the object co$$anonymous$$g towards you it will indeed stop the object as long as you have a variable that deter$$anonymous$$es its speed or motion The l0gic is basically using the mouse input to return a boolean based on the mouse hitting your objects collider when you mouse over that object, with the On$$anonymous$$ouseDown() code attached to it you are saying "when my object is in range of the mouse and has a mouse action attached to it via script, carry out the operation you specified in code when i click the left $$anonymous$$ouse button Down function". arrgh, im waffling, brb with your answer dude..gimme 20 $$anonymous$$s matey Gruffy
Answer by haim96 · Nov 24, 2013 at 01:23 PM
watch this video. it's about raycast laser and fun stuff. the code is c# but should be easy to rewrite in java.
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/fun-with-lasers
Your answer
Follow this Question
Related Questions
Endless runner, road segments spawn speed 0 Answers
How to move a Transform on top of a series of Transforms 0 Answers
How to remove decimals from Vector3.Distance 3 Answers
My character isn't moving 0 Answers
Rubber banding camera (help!) 2 Answers