I'm pretty new to Unity and C# programming, and am trying to get a player-controlled object to do a controlled rotation around the mouse pointer. The functional purpose is to be able to circle-strafe around that pointer and remain facing the pointer, but if the player object's rotation speed isn't fast enough, they can't track it. As a space shooter, I intend to have large and small ships that turn at different speeds, and the intent is to make it harder for larger ships to track smaller objects.
My code right now does everything for movement except control rotation speed. I'm really struggling to get that single element into place. It switches from free rotation smoothly to circle-strafe rotation around the mouse cleanly... it's just that the ship then rotates as fast as I whirl the mouse around it.
I've tried a number of different techniques recommended in searches, but none of them actually work for my scenario when implemented. I'm almost certain I need "Time.deltaTime * v_rotationSpeed" blended into this somewhere, but can't get it working.
I'm hoping someone can help, or at least guide me in the right direction.
using System.Collections;
public class Space2DStrafeMove : MonoBehaviour
{
//movement variables
public float v_speed = 3;
public float v_rotationSpeed = 0.25f;
//private calculation variables
private Vector3 v_cursorLocation;
private float v_lookAngle;
//object variables
private Rigidbody2D v_rigidBody2D;
// Use this for initialization
void Start()
{
//store a reference to the attached Rigidbody2D object
v_rigidBody2D = GetComponent<Rigidbody2D> ();
}
//FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
void FixedUpdate()
{
//Store the current horizontal input in the float moveHorizontal.
float v_moveHorizontal = Input.GetAxis ("Horizontal");
//Store the current vertical input in the float moveVertical.
float v_moveVertical = Input.GetAxis ("Vertical");
//strafe toggle, ship remains oriented towards mouse cursor
if (Input.GetKey(KeyCode.LeftShift))
{
//Establish the mouse position in the camera relative to the position of the ship
v_cursorLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
//Create an angle that the ship will need to move towards in order to look at the mouse cursor
v_lookAngle = Mathf.Atan2(v_cursorLocation.y, v_cursorLocation.x) * Mathf.Rad2Deg;
//Use the two store floats to create a new Vector2 variable movement.
Vector2 v_movement = new Vector2 (v_moveHorizontal, v_moveVertical);
//Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
v_rigidBody2D.AddRelativeForce(v_movement * v_speed);
//Rotate the ship towards the established angle, -90f to account for ship alignment, over time
//Rotates instantly, not desirable
transform.rotation = Quaternion.Euler(0f, 0f, v_lookAngle - 90f);
//turns the ship as fast as I move the cursor, and in the right direction
//transform.rotation = Quaternion.AngleAxis(v_lookAngle-90, Vector3.forward);
//instantly snaps the ship "up"
//transform.rotation = Quaternion.Euler(0f, 0f, v_lookAngle - 90f * Time.fixedDeltaTime) ;
//instantly snaps the ship "up"
//transform.rotation = Quaternion.Euler(0f, v_lookAngle - 90f * v_rotationSpeed * Time.deltaTime, 0f);
//undesirable result
//transform.eulerAngles = Vector3(0, v_lookAngle, 0);
//undesirable result
//transform.rotation = Quaternion.Slerp(v_lookAngle, v_lookDirection.y, Time.time - startTime);
//undesirable result
//transform.rotation = Quaternion.RotateTowards(transform.rotation, v_lookDirection, Time.deltaTime * 10f);
//smooth rotation to the Z axis (flips the ship over)
//transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(v_lookDirection - transform.position), v_rotationSpeed * Time.deltaTime);
//undesirable result
//transform.rotation = Quaternion.AngleAxis(v_lookAngle, Vector3.forward);
//ship spins smoothly over onto it's back
//transform.rotation = Quaternion.Slerp(this.transform.rotation, v_lookAngle, Time.deltaTime * v_rotationSpeed);
}
//this creates smooth up and down, rotate left and right, fly in direction the transform is pointing
if (!Input.GetKey(KeyCode.LeftShift))
{
//Use the two store floats to create a new Vector2 variable movement.
Vector2 v_movement = new Vector2(0, v_moveVertical);
//rotate the transform on the horizontal axis
transform.Rotate(0, 0, -v_moveHorizontal);
//apply force relative to the existing heading of the ship
v_rigidBody2D.AddRelativeForce(v_movement * v_speed);
}
}
}
RigidBody
(justTransform
) so you know what you're about, then attempt to replicate the same setup usingRigidBody
- which could be somewhat more difficult given the need to use finely-tuned rotational forces. \$\endgroup\$