How to implement a transform/move script (JS) + bonus questions

MY AIM

My aim is to click at a chosen point on the map using a raycast (done) and a gameobject (robot/tank) will move to the area you have clicked.

The game in trying to create is a first-person RTS (Real-time-stratagy) where the player can command his army using a first-person camera pointing and clicking where he wants his units (tanks/robots) to move to on the map.

MAIN QUESTION

How do i implement the transform/translate/slerp etc in to the script. I tryed many solutions, looked on youtube, unity answers forums but i am not given the correct result and i do not understand how it works.

Oh and also, do i need a Character controller/Motor controller to move the vehicle (Tank/Robot)

MOVEMENT SCRIPT (JS)

if( Hit.collider.gameObject){
					
					bot.transform.Translate(Vector3.forward * speed * Time.deltaTime); //The final result of the script
					bot.transform.Translate(-Vector3.forward * speed * Time.deltaTime);
					bot.transform.Rotate(Vector3.up, -rotation * Time.deltaTime);
					bot.transform.Rotate(Vector3.up, rotation * Time.deltaTime);
						Debug.LogWarning("Potatoes!"); //Success, the script works!

MY CURRENT SCRIPT (JS)

var bot : Transform; //The robot/gameobject that must follow the raycast
var Range : float; // Range of the raycast
var mesh : GameObject; //Cammea the raycast beam comes from

var Hit : RaycastHit; //Impact/cordinates of the raycast the robot must goto

var speed : float = 0.6; //Speed of the robot
var rotation : float = 0.6; //rotation speed of the robot

//References of how i learnt about raycasting
//http://answers.unity3d.com/questions/15761/click-on-terrain-and-record-vector3-location-i-cli.html
//http://www.youtube.com/watch?v=cZsG4dfCUec
//http://www.youtube.com/watch?v=-F_w3RDRbN4

function Update () {

if(Input.GetMouseButtonUp(1))

var Directionofline : Vector3 = mesh.transform.TransformDirection(Vector3.forward);

	Debug.DrawRay( mesh.transform.position, Directionofline * Range, Color.yellow); //Raycast in visual
   		
		if(Physics.Raycast( mesh.transform.position, Directionofline, Hit, Range)){
		
				if( Hit.collider.gameObject){
					
					bot.transform.Translate(Vector3.forward * speed * Time.deltaTime); //The final result of the script
					bot.transform.Translate(-Vector3.forward * speed * Time.deltaTime);
					bot.transform.Rotate(Vector3.up, -rotation * Time.deltaTime);
					bot.transform.Rotate(Vector3.up, rotation * Time.deltaTime);
						Debug.LogWarning("Potatoes!"); //Success, the script works!
					}
					}
					}

BONUS QUESTION

For the raycast i noticed there is something call a “screenPointToRay”. Can i use that in place of my current raycast section?

var Directionofline : Vector3 = mesh.transform.TransformDirection(Vector3.forward);

	Debug.DrawRay( mesh.transform.position, Directionofline * Range, Color.yellow); //Raycast in visual
   		
		if(Physics.Raycast( mesh.transform.position, Directionofline, Hit, Range)){

READ BEFORE POSTING

  1. If you are going to post any links please explain it with your own words, the explanation in the link may not be explained well enough for me to understand.
  2. Feel free to modify the script i have provied, although i would be very happy if you would explain a few things.
  3. Currently im in college so im no expert in coding, i am currently learning the theory.
  4. NO GOOGLE IT’S as i already have. We must move forward, not back. :slight_smile:

Appologies if ive been abit rude! :frowning:

FINALY

Thank you for your time!

Well I am GLAD to have the honor to be allowed to answer your questions! I really feel like some kind of “chosen one” to obey your rules in order to be allowed to help you. :wink:

Just a quick tip: It’s your question but other people’s rules. It’s that easy. I don’t want to be rude but you can’t dictate anybody to follow these rules just to make your life easier. It’s actually your job as a programmer to educate yourself, read through the documentations and all that stuff and write a solution to the problems. The community is here to help, no question, but it’s not here to make sure you don’t need to think for yourself anymore.

Nevertheless. Pathfinding is not always the easiest thing. Good for your that there are already a couple of frameworks around. One of the best and most used one certainly is the A* pathfinding algorithm. There’s a pretty good implementation for Unity:

http://arongranberg.com/astar/

I am sure the description, examples and documentation are good enough to match your high standards. :wink:

Haha yes a big honour indeed! Plus you get a gold medal to! :slight_smile:

Yeah im sorry for that, i havnt had the best of times asking for help on forums and it sometimes gets frustrating trying to figure things out, so i try to not to use a forum as i tend to be very rude due to the frustration.

My appologies.

Anyway! So this pathfinding is basicly what i need for my little robot to get from A to B when i click on a random area of the map. I was expecting something along the lines of…

bot.translate.position = (Hit.point) speed * Time.deltaTime;

Not propper code that makes sence, but somewhere along those lines.

I’ll look into this pathfinding

No problem! :slight_smile:

Well sending an object from A to B over time is not really difficult. That’s basically done with a simply Translate over time as you did it in your script.

However, there are situations which are far more complex than that. E.g. what happens if there is no direct path to point B? What happens if you get a height difference, any obstacles or unwalkable ground? These are all things which could get quite hard to build from scratch and require a lot of work to be put into. A* is doing all that directly and I have build that already into two (not publicly visibile) projects of mine. So I definitely recommend that.

there is an example of using raycasts on the mousePosition reference page: Unity - Scripting API: Input.mousePosition

you’ll need to tweak it slightly to the raycast returning a “hit” and then instantiating the object at the hit position… but it’s a good quick learning example of how to get the mouse input to work with objects in a scene.

I’d advise laying out your code a bit more consistently with what it’s doing; it’ll make things clearer when they go wrong :slight_smile: ‘{ }’ are used for scope so it’s usual to indent each scope block for example ( Scope (computer science) - Wikipedia )

Also be careful with your if syntax, your mousebutton check isn’t working how I think you want it because it’s missing it’s ‘{ }’. Without the braces you only get one statement… if you want a whole block to be driven by the if you need to use the braces

function Blah()
{
	if(...)
	{
		...;
		...;
		if(...)
		{
			...;
			...;
		}
	}
	
	if(...) ...;
	
}

Hmm very good point!
Ive already downloaded A* pathfinding and unfortunately i have 2 errors of the same problem (on seperate lines) ive already gone looking for a solution still am.

I’ll leave the problem incase its something youve came across.
Thanks for the help so far!

I expect is something to do with a varible from my experience with VB (Visual Basic)

You need a newer version of Unity.

–Eric