Moving an object in a desired line.

Hi again I’m still having problems trying to solve this and have been unable to describe my problem in a way that anyone could help me so i drew a diagram that might help you help me :slight_smile:

(i) Main Character

(ii) Rocket Weapon attatched to main character with a spawnPoint object infront of the barrel to spawn the rockets, They are on a lookat script to always have the rotation oriented towards the crosshair

(iii)Crosshair is attatched to the mouse orbit camera that controls the characters rotation and the crosshairs vertical position.

(iv)Rocket is produced at spawnPoint on Fire1 press, I have managed to get it going in the correct rotation as if it was to do the desired path but it isnt going on the correct line.

(v)The desired path for the rocket to take, starting at spawnPoint and moving along the line from spawnPoint towards crosshair to pass through it correctly.

(vi)The current path that the rockets take. Always staying at a steady forward rate as I don’t know how to enter the correct path.

My current code for the rocket is;

var rocketPrefab:Transform;

function Update () 
{
	if(Input.GetButtonDown("Fire1"))
	{
		var rocket = Instantiate(rocketPrefab, 
										GameObject.Find("spawnPoint").transform.position, 
										GameObject.Find("spawnPoint").transform.rotation);
		rocket.rigidbody.AddForce(transform.forward *2000);
		
	}
}

Where I need transform.forward to be replaced by the path from spawnPoint to crosshair somehow. Am I going about this completely the wrong way?

I hope I have explained this better this time as this problem is really holding back my development as I want my basic functionality and knowhow working before I start on the more complicated ideas.

Any help or contributions appreciated :slight_smile: Thank you for taking your time to read this :slight_smile:

Sir MaXx

Try replacing the last line of your script:

"rocket.rigidbody.AddForce(transform.forward *2000); "

with something like :

“rocket.rigidbody.velocity.z = 400;”

and add the following inside Update function also:

“rocket.transform.LookAt(crosshair);”

You will need to define the crosshair variable to a Transform in order to work.

I don´t know if it will work, sorry. :slight_smile:

Thanks for the feedback, The first line didn’t seem to change anything in the movement, it just seemed to make it a bit jumpier. This problem doesn’t seem to be as simply solved as I thought it might be :S

The rocket code seems to be OK, but there is likely a problem with the crosshair code. I don’t know what approach you are using (maybe post your code?) but a typical way to implement a crosshair is with a raycast from the camera. This thread gives an example of how to do this. The RaycastHit object returned by the raycast has information about the point in space where the hit occurred (ie, the point the crosshair was targetting). Once you have this point, you can use transform.LookAt to make the launcher point toward it. The code would probably look something like:-

function Update () {
	var hit: RaycastHit;
	var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	
	if (Physics.Raycast(ray, hit)) {
		transform.LookAt(hit.point);
	}
}

This should be attached to the gun object and will point at the crosshair as long as it is over a collider.

Oh thanks for this I was actually reconsidering my cross hair way this morning as its very inaccurate after the point in space it is. I will definitely be having a look into your stuff on ray casting and giving it a try :slight_smile: