Move script

Hello guys,

Could someone writte me simple script that moves object to certain node (empty object)?
I would just like that fish would move from left side of screen to right side only on X coordinate.

I am not programmer and i would really need it to build up demo prototype.

var Fish : GameObject;

function Start(){

Fish = GameObject.Find(“The Name Of Your Fish Asset Goes Here”);

}

function Update(){

if(Input.GetKey(“right”)){
Fish.transform.position.x +=1;
}

if(Input.GetKey(“left”)){
Fish.transform.position.x -=1;
}
}

untested code but give it a try :slight_smile:

Oh sorry i forgot to add that i dont want this on player fish. It is “enemy fish”. So it just should move from right side to left side of screen with some speed. Could you writte this kind of script please? And there is no need to call certain GameObject for it i guess.

var enemyFish : GameObject;

function Start()
{

enemyFish = GameObject.Find(“The Name Of Your Fish Asset Goes Here”);

}

function Update()
{

transform.Translate(Vector3.left* 10 *Time.deltaTime);

}

Well I’m not sure if you need a GameObject or not, try it out, if you don’t need a GameObject just remove every code before function Update it should work I think.
p.s
(Vector3.left* 10 *Time.deltaTime); 10 is the speed the fish will move so change it to whatever speed you want the fish to move at.

It doesnt work. I used it from function Update, but it doesnt move at all.

oh I forgot to mention that you need to attach a Rigidbody to your enemy fish which you can do by selecting the enemy fish going to Component->Physics->Rigidbody after you attach it disable gravity make sure to check all the boxes in the Constraints section.

Hopefully that will make it work :slight_smile:

Ah still doesnt work. That is javascript language right?

Lol wow that sucks :frowning:
Yeah it’s Javascript.

enemyFish.transform.Translate(Vector3.left* 10 *Time.deltaTime);

Maybe try that ?

just to make sure, you did attach the enemyFish asset to the script in the Script properties right ?

Works but it moves very mad! Even if i change variable…

Oh! Well that’s 'least some progress eh lol.
What do you mean by it moves mad anyways ? What does it exactly do ?

It moves extremly fast and it moves just wierd random on some dirrection.

hmm, well try changing the vector3.left to something different like vector3.right or vector3.up or vector3.down maybe change the speed variable to 1 or so if not a decimal value might help

Anyone mind help to create me some simple movmenet script from right to left side of screen? (X coords)

See transform.Translate and Vector3.Lerp for some very simple example scripts.

It sounds like you want to build an AI script rather than just a move script. You say it is an “Enemy Fish”, this kind of tells us that it is not going to be nice for our player. Question is, is this a Frogger type of game, where the fish just constantly move in one direction or the other, or does it actively seek out the player?

Sadly, I just wrote a writeup on basic AI function for a zombie, but it actually can be used anywhere you have a seek and destroy AI needed.

http://forum.unity3d.com/threads/81841-A-quick-question…?p=523570&viewfull=1#post523570

As far as the script from above… You would probably want to use Mathf.Repeat for cycling and Mathf.PingPong for ping ponging.

//This script is placed on the fish....
var fishSpeed : float = 10.0; // number of seconds it takes to get from min to max
var yPlane : float=0.0;// position on the screen up and down.
var minX : float=-20.0;// from here
var maxX : float=20.0;// to here

function Update()
{
	var x=minX + Mathf.Repeat(Time.time / fishSpeed * (maxX-minX), (maxX-minX));
	//print(Array(Time.time / fishSpeed, Time.time * (maxX-minX), fishSpeed, maxX-minX));
	transform.position=Vector3(x, yPlane, 0);
}

this should help a great deal. :slight_smile: enjoy!