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][1]. 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();
}
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
[1]: /storage/temp/81364-testinput.zip
↧