- Home /
Mouse Position turning 3rd person space sim.
I am triyng to make a space exploration game, i want to use the mouse controls to turn the ship, the same sistem used in games as BattleStarGalactica Online, in which the position of the mouse in relation to the center of the screen is used to make the ship turn. I tried making that, but commands like "Input.mouseposition" are really tricky for me to completely understand, and once i use them there is the problem of triyng to redirect the origin of the mouseposition vector2 to the center of the screen. I already have done the trust forward and backwards :
public class NavigationScript : MonoBehaviour {
public float translation = 0F;
void Update () {
if (Input.GetAxis ("Mouse ScrollWheel") > 0)
{
if (translation <= 0.5F) {translation+=0.1F;}
}
else if (Input.GetAxis ("Mouse ScrollWheel") < 0)
{
if (translation >= -0.4F) {translation-=0.1F;}
}
transform.Translate(0, translation, 0);
}
}
I am still a beginner, can anyone help me, or at least give me some tips(or even one good tutorial), in making this turning algorithm (in C# plz) ?
Answer by AlwaysSunny · Jun 22, 2015 at 05:51 AM
There are several ways to approach this requirement. Assuming you want the ship to turn faster the further the mouse is from the screen center, this should get you started:
public Camera mainCamera; // assign your main camera
public float rateOfRotation = 10f;
void Update() {
Vector3 lookVector = mainCamera.ScreenPointToRay(Input.mousePosition);
Quaternion desiredRotation = Quaternion.LookRotation( lookVector, transform.up );
transform.rotation = Quaternion.Lerp( transform.rotation, desiredRotation, rateOfRotation * Time.deltaTime );
}
There are several ways to improve upon this, like creating a deadzone or maximum radius considered, but it should give you a good place to start.
I made some alterations on your code because it wasn't translating: Vector3 lookVector = mainCamera.ScreenPointToRay(Input.mousePosition);
from a Ray to a Vector3, so I did :
public class NavigationScript : $$anonymous$$onoBehaviour {
public Camera $$anonymous$$ainCamera;
public float translation = 0F;
public float LookSensitivity = 1F;
public float LookSmothDamp = 0.1F;
void Start () {
}
void Update () {
Ray lookRay = $$anonymous$$ainCamera.ScreenPointToRay(Input.mousePosition);
Quaternion desiredRotation = Quaternion.LookRotation (lookRay.direction, transform.up);
transform.rotation = Quaternion.Lerp( transform.rotation, desiredRotation, LookSensitivity * Time.deltaTime * LookSmothDamp );
if (Input.GetAxis ("$$anonymous$$ouse ScrollWheel") > 0)
{
if (translation <= 0.5F) {translation+=0.1F;}
}
else if (Input.GetAxis ("$$anonymous$$ouse ScrollWheel") < 0)
{
if (translation >= -0.4F) {translation-=0.1F;}
}
transform.Translate(0, translation, 0);
}
}
But the controls are very chaotic and I don´t know what to do to adjust them in a meaningful way, could you please take a look and help me do some "final" adjustements to this basic controls ?
Not sure why I thought that approach was superior to this simplified version. Guess I'm so used to thinking in 3D, I go for that even when 2D is easier.
void Update () { CamLook(); }
public float lookRate = 5f;
public float maxEnvelope = 0.2f;
public float deadZone = 0.0001f;
void CamLook() {
float xD = Input.mousePosition.x - (Screen.width*0.5f);
float yD = Input.mousePosition.y - (Screen.height*0.5f);
Vector3 v = new Vector3( -yD, xD, 0 ) * 0.001f;
if (v.sqr$$anonymous$$agnitude < deadZone) return;
v = v.normalized * $$anonymous$$athf.$$anonymous$$in( v.magnitude, maxEnvelope );
transform.Rotate( v * lookRate * Time.deltaTime );
}
Your answer
Follow this Question
Related Questions
Can't shoot towards mouse click point 1 Answer
How to set up a space game with Evochron: Mercenary like controls? 0 Answers
Unity Ui. Why constant pixel size option in Canvas Scaler don't work with mouse? 1 Answer
Move object towards mouse in full 3D space 2 Answers
Turning animation using mouse value 0 Answers