smooth problem

hi

Im working now on a flight project and I need some help. My jet shoud fly smooth in the air but it dont work as I want. The jet flies vertically instead of horizontally.

You might dont understand what I mean thereforce I have here some screenshoots (so and it shoudnt be so):

585701--20892--$screenshoot1.png

585701--20893--$screeenshoot2.png

Heres the code I use:

var coefficient : float = 0;
var aircraft : GameObject;
var jetfgc : GameObject;

function Update () {
	if(renderer.enabled) {
	this.transform.parent = null;
	
		 var rotation = Quaternion.Slerp(transform.rotation,aircraft.transform.rotation,Time.deltaTime*coefficient); // I also tried with rotation.x-90 but it gives me afterwards a unusable result.
   
transform.rotation.x = rotation.x;
transform.rotation.y = rotation.y;
transform.rotation.z = rotation.z;
//transform.Rotate(-90,0,0); // This is not important.

transform.position =jetfgc.transform.position;
	}
}

And at some time the jet dont rotate.

The script is placed as a child of: jet → fighter5forunity → flyjet

Here is a demo project with my jet you can adujst all what is to adjust and afterwirds I would be happy :smile: when you can post the correct version.

Thanks

realm_1

585701–20894–$testproject.part3.rar (553 KB)
585701–20895–$testproject.part2.rar (1.43 MB)
585701–20896–$testproject.part1.rar (1.43 MB)

Have nobody a solution for that?

Come on I need this solution really.

Thanks

unity’s idea of what is forward is not your idea. First make a gameobject then put your mesh inside that gameobject and rotate the mesh so the orientations are correct. Finally, do your math and movement on the gameobject instead. Then unity will understand which way is forwards and which way is up. And it will be trivial easy to correct by manually rotating the mesh in the editor because you do your operations on the gameobject parent instead in game.

Ok Ive tried to do that but it doesnt work neighter. And the mesh jumps always out of the empty GameObject.

I think I am confused by what you are trying to accomplish. What do Aircraft and Jetfgc refer to, and why are you using something else to control your rotations. What controls the movement and rotation of the plane? (where are your axis controls)

I would suggest writing this script as a full plane controller. This will make it easier for anyone who is working with you on it to help you.

OK, saying this, I tinkered with the script you had, changed it up a bit and made it into a controller, rather than a responder.

I have probably added in alot more features then what you originally had.

First, I used a Plane and mouse ray to figure the direction I wanted to be, then calculated a look rotation based off of that.

Next I adjusted the rotation of the vehicle based on it’s current rotation and the desired rotation.

I changed the velocity based on its heading and speed factor.

Lastly I added in the angular orientation from the difference in X translated to Z and gave it a new Z rotation based off of the current rotation to a desired rotation.

One thing is that the speed dictates the distance of the plane, meaning that the orientation of the target point is farther away when you are traveling faster. (you can’t turn fast at high speeds)

Mouse left speeds you up, mouse right slows you down.

var damping : float = 2.0;
var speed=10.0;
var speedChange=2.0;
var minSpeed=10.0;
var maxSpeed=100.0;

function Start(){
	if(!rigidbody)
		gameObject.AddComponent(Rigidbody);
	rigidbody.interpolation=RigidbodyInterpolation.Extrapolate ;
}

function Update () {
	if(Input.GetButton("Fire1"))speed=Mathf.Clamp(speed + speedChange * Time.deltaTime, minSpeed, maxSpeed);
	if(Input.GetButton("Fire2"))speed=Mathf.Clamp(speed - speedChange * Time.deltaTime, minSpeed, maxSpeed);
	
	var plane : Plane=Plane(transform.forward, transform.position + transform.forward * speed);
	var ray : Ray=Camera.main.ScreenPointToRay(Input.mousePosition);
	var dist : float;
	plane.Raycast(ray, dist);
	
	var target=ray.GetPoint(dist);
	var relativePos = target - transform.position;
	var p1=Vector2(transform.forward.x, transform.forward.z);
	var p2=Vector2(relativePos.x, relativePos.z);
	var angle=Vector2.Angle(p1, p2);
	angle=Vector3.Dot(transform.right, relativePos) > 0 ? -angle: angle;
	var lookRotation = Quaternion.LookRotation(relativePos);
	var rotation=Quaternion.Slerp(transform.rotation, lookRotation, damping * Time.deltaTime);
	
	rigidbody.velocity=transform.forward * speed;
	rigidbody.angularVelocity*=0.5;
	transform.rotation=rotation;
	var z=transform.localEulerAngles.z;
	if(z>180)z=z-360;
	transform.localEulerAngles.z=Mathf.Lerp(z,angle, damping * 5 * Time.deltaTime);
}

The cool thing is that I stepped away from the actual translation into rigidbody movement and maintained the current orientation to the new one. So when it hits an object it reacts to physics. :wink: I know it looks kind of complicated but if you take the time you will see that at every step I am setting rotations based off of the current rotations. This makes it a bit more believable.

For a camera, I simply attached a smooth follow camera with a zero height.

Thanks for your script.

So I know that what I’ve created is a little bit mess. For rotating I use the mouse look script and for the movement my fly script. So there I haven’t got any axis I know but that I saw not till I was finished with the script. There I use instead of Input.GetAxis(“Horizontal”) Input.GetKey(“w”). The jetfgc GameObject is here for my own smooth script so that the jets position is as the jetfgc position because I make only the rotation smooth and I have also to sync the position. But I think it a little bit confused, and I dont understand now so excactly what i’ve done thereforce I searched for an other solution.

Anyway I tested your script I made my aircraft mesh as a child of the camera and attached your script on the camera. The SmoothFollow script I also attached to the camera.

But now it gives me an NullReferenceException at this line

var ray : Ray=Camera.main.ScreenPointToRay(Input.mousePosition);

But the script is on the camera so where is the error?

So I solved the problem creating a variable cameraf as Camera.
And then I wrote:

var cameraf : Camera;
function update() {
var ray : Ray=cameraf.ScreenPointToRay(Vector3(Input.mousePosition.x,Input.mousePosition.y,0));
}

The script is on the mesh and so the camera has to be defined in the script.
The camera isn’t a child of the mesh.

But now there are some other problems:

  • The plane can’t accelerate.
  • The plane begins to vibrate when I look up. (so it have a little acceleration at the begin therefore I can a little bit move.).
  • The terrain vibrate when I look up or when I look down. (But not so force as the plane only a little bit.).
  • When I fly vertically I can’t look up again.

Do you now why is that so? I mean do you have a solution for that?

Sorry that I said first your script doesn’t work so good. I don’t declare the Fire1 and Fire2 Buttons in the inspector. Sorry about that. Now it work very good and the vibe on the terrain makes it a little more realistic.

So want I wanna say is: Thanks for that. :smile:

But one last question:
Could you adapt me the script so that it only rotates the plane and not moving?

Because I’m not very good in coding.

Thanks

realm_1

So, you just want to disable the movement?

Just remove this line:

rigidbody.velocity=transform.forward * speed;

Ok

It works fine on my test project, but not on the real project, so I’ll set the plane there new and it will work. :smile:

So and one question I’ve got at the testing of that all:

What should this line here do:

rigidbody.interpolation=RigidbodyInterpolation.Extrapolate ;

Thanks

realm_1

It changes the way that the rigid body reacts to the forces on it by smoothing the reaction. Supposedly it is meant to fix camera jitter, but as much as I have tested it it does not.

Yeah I agree. I think thats why the terrain vibes.