Spaceship 3D Movement

Hey,

I’ve searched this forum a bit, but I didn’t find what I’m looking for …

I already have a script that is not working - for no reason, in my opinion.

I got a Spaceship at position 0, 0, 0 facing forward into the z-axis
I’ve set a fixed destination x,y,z

Now I want it to move and rotate to the destination.

I set up a plane from 3 points.

  1. The ships position
  2. The ships position + forward vector of the ship
  3. The destination

Then I take the normal vector of this plane and tell my ship to rotate around the normal vector, again and again and again.

It should work in my opinion. I’m not bad at math, but vectors aren’t my favorite thing.
Anyway it should work.

Any ideas what I’m doing wrong ?
My ship is just making spirals all the time …

Youll need to show us your codes.

// Move Command. Called within Update()
private void Move() {

	// If the destination is not reached
	if (destination != Vector3.zero  transform.position != destination) {
		// This is the vector from the ship to the destination
		Vector3 direction = (destination - transform.position).normalized;

		// As long as the ship is not facing the destination point, it has to turn. Else it just has to move forward
		if( ! directionLocked ) {

			// This is the angle between the "facing" of the ship and the destination
			float angle = Vector3.Angle(direction, transform.forward);

			// Here a plane is being created by 3 points
			// Position of Ship, Destination, Position of Ship + a bit forward
			Plane plane = new Plane(transform.position, destination , (transform.position + transform.forward));

			// The normal vector on the plane
			Vector3 axis = plane.normal;

			// The ship has to turn in one direction. Inside this if statement the "rotating direction" is calculated
			// If this rotation direction is not calculated
			if(!choseDirection) {
				Vector3 cross = Vector3.Cross(transform.forward, direction);
				float dot = Vector3.Dot(cross, axis);
				
				if(dot < 0) {
					currentRotationSpeed = -rotationSpeed;
				} else {
					currentRotationSpeed = rotationSpeed;
				}
				// Now the rotation direction is chosen. So set this value to true 
				choseDirection = true;
			}

			// If the angle is greater than the rotationSpeed then the ship has to rotate
			if(angle > rotationSpeed) {
				// Rotate around the normal vector of the plane
				transform.Rotate(axis, -currentRotationSpeed);
			// If the angle is smaller, then the ship will focus the destination immediately
			} else {
				transform.LookAt(destination);
				// Now set the boolean to true, because the ship doesnt have to turn anymore
				DirectionLocked = true;
			}
		}

		// Move forward
		transform.rigidbody.velocity = transform.forward * speed;
		
		// If the actual distance is below "stoppingRange" then the destination is set to zero, so that the ship stops to move
		if ( Vector3.Distance(transform.position, destination ) < stoppingRange)
			destination = Vector3.zero;
	} else {
		// Target reached
		transform.rigidbody.velocity = Vector3.zero; 
		DirectionLocked = false;
		choseDirection = false;
}

Vectors/Rotations always do my head in…

  1. transform.position != destination. This condition will probably never be met. Trivial problem though as I see you do a distance test later on and set the destination to zero which should be met by the initial condition (destination != Vector3.zero)

  2. if(angle > rotationSpeed)

I it would work better with

 if(Mathf.Abs(angle) > 1)

Vectors always drive my head crazy, too, specially in 3D = /

 if(Mathf.Abs(angle) > 1)

Yes, you have a point.
Because the angle might be -15 degrees or so on. I got to check that
Tomorrow :wink:

Didnt work …
Still crappy in most cases.

Somtimes it works, but usually not