- Home /
Top-down space flight physics - AddForce - Object moves off center the faster it goes
Hi all,
I just started a little project where I want the player to control a ship from a top-down perspective. I want the whole thing to use physics-based movement.
My problem is that when I add force to a rigidbody, the object it is attached to seems to move off its position the more the faster it goes. However, transform.positions seem to be alright.
I put together this little package. If you press W, the ship will move upwards and off the center the longer you press W. S moves in the other direction.
The code is rather simple:
using UnityEngine;
using System.Collections;
public class TestController : MonoBehaviour {
public float mainThrust = 100;
private Vector3 cameraOffset = new Vector3(0f, 0f, 150f);
private new Rigidbody rigidbody;
void Start () {
rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate() {
float axisVertical = Input.GetAxis("Vertical");
Vector2 force = new Vector2(0f, axisVertical) * mainThrust * Time.fixedDeltaTime;
rigidbody.AddRelativeForce(force, ForceMode.Impulse);
Camera.main.transform.position = rigidbody.transform.position - cameraOffset;
Debug.Log("camPos: " + Camera.main.transform.position
+ " - rbPos: " + rigidbody.transform.position);
}
}
Any help appreciated.
Thanks Nico
Video for clarification on YouTube: https://youtu.be/12z526xrUp$$anonymous$$
Your answer
Follow this Question
Related Questions
Update speed and physics makes my rigidbody jiggle 2 Answers
move player to its rotating direction? 2 Answers
adding force 1 Answer
Prevent Rigidbody From Rotating 3 Answers
Moving an object using rigidbody.AddForce & keyboard input. 1 Answer