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 :smiley:

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, 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 :)

any more information on this?

4 Answers

4

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)
:slight_smile:
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... :)

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!

Gruffy,
It would not work??

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