How to add rotation and U Turn System to a Space Ship

Hello everyone,
I am creating a Space RTS Game with Top Down View. When I right click on screen, my ship go to that position. My ship is actually always sliding in a 3D Object. But right now I didn’t added rotation in ship. It always facing in the same direction. I tried many things but problem is far more complicated for me than I imagined.
I want to add Rotation to My ship and also U Turn System. So it don’t just go backward, instead take U Turn also when needed.
Sorry for my Bad English and lack of explaining skill. I hope everyone will understand my problem. Please help.

To help you out we would need to see your actual script :slight_smile:

1 Like

Here’s a little package demonstrating the typical use of turning something towards a target. See attached package.

6041018–652844–MissileTurnTowards.unitypackage (11.3 KB)

1 Like

How you would approach this would depend on if you want realistic behavior, or Star Trek / Star Wars behavior.

For a realistic approach, you would rotate the ship while continuing in the same direction and apply thrust in the direction it is now facing. So a U turn would be rotate the ship 180 degrees, then apply thrust forward. This will slow the ship down eventually to a stop and then start moving in the direction it is facing.

For a typical sci fi movie behavior, you’ll act like it is a large sea vessel or aircraft. My approach has been you write code to quickly bleed off any sideways velocity of the ship and apply a force in the forward direction. So when you rotate the ship it doesn’t slide much (though still a little or it won’t look that realistic). YMMV

1 Like

@Terraya @Kurt-Dekker @Joe-Censored
Thank you so much to all for replies.

This script handle ship movement and rotation:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Agent : MonoBehaviour
{
    public float maxSpeed = 1.0f;
    public float trueMaxSpeed;
    public float maxAccel = 30.0f;

    public float orientation;
    public float rotation;
    public Vector3 velocity;
    protected steering steer;

    public float maxRotation = 45.0f;
    public float maxAngularAccel = 45.0f;

    base_behavior bb;
    GameObject target;
 

    // Start is called before the first frame update
    void Start()
    {
        velocity = Vector3.zero;
        steer = new steering();
        trueMaxSpeed = maxSpeed;

        bb = gameObject.GetComponent<base_behavior>();
        target = bb.waypoint;
    }

    public void SetSteering(steering steer, float weight)
    {
        this.steer.linear += (weight * steer.linear);
        this.steer.angular += (weight * steer.angular);
    }

    // change the transform based off of the last frame's steering
    public virtual void Update()
    {
        Vector3 displacement = velocity * Time.deltaTime;
        displacement.y = 0;

        orientation += rotation * Time.deltaTime;

        //limit orientation between 0 and 360
        if(orientation < 0.0f)
        {
            orientation += 360.0f;
        }
        else if(orientation > 360.0f)
        {
            orientation -= 360.0f;
        }
        transform.Translate(displacement, Space.World);
        transform.rotation = new Quaternion();
        transform.Rotate(Vector3.up, orientation);
    }

    //update movement for the next frame
    public virtual void LateUpdate()
    {
        velocity += steer.linear * Time.deltaTime;
        rotation += steer.angular * Time.deltaTime;
        if(velocity.magnitude > maxSpeed)
        {
            velocity.Normalize();
            velocity = velocity * maxSpeed;
        }

        if(steer.linear.magnitude == 0.0f)
        {
            velocity = Vector3.zero;
        }
        steer = new steering();
        transform.LookAt(target.transform.position);
    }

    //used for speed matching when traveling in groups
    public void speedReset()
    {
        maxSpeed = trueMaxSpeed;
    }
}

Currently I am using:

transform.LookAt(target.transform.position);

for rotation in above code.