How to move an object on a 2d plane forward, backward, and in a rotation about the mouse position?
Hullo commUnity,
I'm a programming student trying to put together a top-down 2d space-fighter game for school / portfolio (think Galak-Z, but with mouse and keyboard instead of sticks). I need the physics to feel real, and so far I've got them working the way I want. I just can't have instant motion canceling allowable.
So, the question is a little deceptive. I have my script doing both of those things, in the manner in which I want them to. The problem I'm having is, when rotating around the mouse, the value changes result in an instant canceling of motion along the previous vector, making the rotation vector capable of stopping all momentum, to include momentum generated by the forward/backward function.
The problem I feel is in the fact that I have to set lateralVelocityVector = new v3... instead of lateralVelocityVector += new v3... The issue with the second is that it results in a spiraling outward motion, instead of a locked circle around the mouse.
Halp plz!
public float thrust;
public float lateralThrust;
public Vector3 forwardVector;
public Vector3 lateralVector;
public Vector3 forwardVelocityVector;
public Vector3 lateralVelocityVector;
public Vector3 velocityVector;
public float maxSpeed;
public float minSpeed;
void Start () {
forwardVector = new Vector3 (0,0,0);
lateralVector = new Vector3 (0,0,0);
velocityVector = new Vector3 (0,0,0);
maxSpeed = 40;
minSpeed = -30;
thrust = 0.1f;
lateralThrust = 10f;
}
void Update(){
Vector3 position = Camera.main.WorldToScreenPoint(transform.position);
Vector3 direction = Input.mousePosition - position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis((angle -90), -Vector3.up);
}
void FixedUpdate () {
if (Input.GetKey ("w")){
/*Get the forward vector while the button is pressed
* and store it. Set transform vector to forward vector
* plus lateral vector and move toward it.
*/
forwardVector = (transform.forward * Time.deltaTime) * thrust;
float clampedX = Mathf.Clamp (forwardVector.x, -1, 1);
float clampedZ = Mathf.Clamp (forwardVector.z, -1, 1);
forwardVelocityVector += new Vector3 (clampedX, 0, clampedZ);
} else if (Input.GetKey("s")){
forwardVector = (-transform.forward * Time.deltaTime) * thrust;
float clampedX = Mathf.Clamp (forwardVector.x, -1, 1);
float clampedZ = Mathf.Clamp (forwardVector.z, -1, 1);
forwardVelocityVector += new Vector3 (clampedX, 0, clampedZ);
} else {
float clampedX = Mathf.Clamp (forwardVector.x, -1, 1);
float clampedZ = Mathf.Clamp (forwardVector.z, -1, 1);
forwardVelocityVector += new Vector3 (clampedX, 0, clampedZ);
}
if (Input.GetKey ("a")) {
lateralVector = (-transform.right * Time.deltaTime) * lateralThrust;
float clampedX = Mathf.Clamp (lateralVector.x, -1, 1);
float clampedZ = Mathf.Clamp (lateralVector.z, -1, 1);
lateralVelocityVector = new Vector3 (clampedX, 0, clampedZ);
} else if (Input.GetKey ("d")) {
lateralVector = (transform.right * Time.deltaTime) * lateralThrust;
float clampedX = Mathf.Clamp (lateralVector.x, -1, 1);
float clampedZ = Mathf.Clamp (lateralVector.z, -1, 1);
lateralVelocityVector = new Vector3 (clampedX, 0, clampedZ);
} else {
float clampedX = Mathf.Clamp (lateralVector.x, -1, 1);
float clampedZ = Mathf.Clamp (lateralVector.z, -1, 1);
lateralVelocityVector = new Vector3 (clampedX, 0, clampedZ);
}
velocityVector = forwardVelocityVector + lateralVelocityVector;
transform.position += velocityVector;
}
Sorry about the formatting. First question, and not familiar with posting here ;p
So, I eventually went with a rigidbody and AddForce to accomplish my goal. Still, I wouldn't $$anonymous$$d a vector-based solution, if some super-genius has one up their sleeve. In any case, here's the code for the solution :)
using UnityEngine;
using System.Collections;
public class FlightController : $$anonymous$$onoBehaviour {
public float thrust;
public float lateralThrust;
public Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody> ();
}
void Update(){
Vector3 position = Camera.main.WorldToScreenPoint(transform.position);
Vector3 direction = Input.mousePosition - position;
float angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis((angle -90), -Vector3.up);
}
void FixedUpdate () {
if (Input.Get$$anonymous$$ey ("w")){
rb.AddForce(transform.forward * thrust);
} else if (Input.Get$$anonymous$$ey("s")){
rb.AddForce(-transform.forward * thrust);
}
if (Input.Get$$anonymous$$ey ("a")) {
rb.AddForce(-transform.right * lateralThrust);
} else if (Input.Get$$anonymous$$ey ("d")) {
rb.AddForce(transform.right * lateralThrust);
}
}
}
Rigidbody is the better approach. But for Vector based movement you want to Normalize
This should avoid outward spiralling (provided you dont use += which will just keep adding to the value)
Your vector based method would not properly handle collisions.
Your answer
Follow this Question
Related Questions
Using quaternions to align game objects. 1 Answer
How can I bind this script into the state of my camera? 2 Answers
Grabbing the Relative eulerAngles.y of a Rotation 1 Answer
Rotate object in 3d space along one axis in the direction of movement vector. 0 Answers
Vector3 and transform.rotation? 2 Answers