- Home /
Question by
chemicalvamp · Oct 19, 2011 at 12:11 AM ·
rotationphysicsforce
3D Space thruster force problem.
Simple spaceflight force, it needs tweaking.. But my problem is that it always adds the force applied relative to world, and not local rotation. How can I fix that?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class PhysicsFlyer : MonoBehaviour
{
public float maxThrust = 15f;
public float maxStrafe = 10f;
public float engineForce = 2f;
public float maxVelocityChange = 2f;
public float accelerationSmooth = 20;
private Vector3 velocityChange;
void FixedUpdate()
{
velocityChange = new Vector3(Input.GetAxis("Horizontal") * engineForce, Input.GetAxis("Vertical") * engineForce, 0);
if (velocityChange.magnitude > 1)
velocityChange = velocityChange.normalized;
velocityChange += new Vector3(0, 0, Input.GetAxis("Mouse ScrollWheel") * engineForce * 10);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxStrafe, maxStrafe);
velocityChange.y = Mathf.Clamp(velocityChange.y, -maxStrafe, maxStrafe);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxThrust, maxThrust);
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
}
}
Comment
Best Answer
Answer by syclamoth · Oct 19, 2011 at 12:42 AM
You can always use
Vector3 globalVelocity = transform.TransformDirection(velocityChange);
which uses your transform's inbuilt ability to translate between local and global coordinates!
Your answer
Follow this Question
Related Questions
Add force at position with respect to rotation 1 Answer
Add torque is not behaving same in opposite Y direction. Need help! 1 Answer
Returning a rigidbody back to its original x and z rotations through physics forces. 2 Answers
Rotating a Rigidbody with Physics 1 Answer
Simulating the graphics of a rolling 3D ball in a 2D game 2 Answers