Search Unity

Space ship movement

Discussion in 'Physics' started by Shadowsektor, Oct 29, 2019.

  1. Shadowsektor

    Shadowsektor

    Joined:
    Jul 9, 2017
    Posts:
    6

    This image from stackoverflow thread perfectly shows what I need, but code from thread doesnt helps me :(. More conditions: ship can't do backward motion, using x, z coordinates (2d in 3d). Here is the code what i'm using, but it doesnt do all things what i'm need.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [DisallowMultipleComponent]
    6. public class Movement : MonoBehaviour
    7. {
    8.     // how fast the player moves.
    9.     [SerializeField][Range(0.01f,2f)]
    10.     public float speed = 0.05f;
    11.  
    12.     // where we want to travel too.
    13.     private Vector3 targetPosition;
    14.     // toogle to check track if we are moving or not.
    15.     private bool isMoving;
    16.    
    17.     // a more visual description of what the left mouse button code is using
    18.     const int LEFT_MOUSE_BUTTON = 0;
    19.  
    20.     /// <summary>
    21.     /// Use this for initialization.
    22.     /// </summary>
    23.     void Start()
    24.     {
    25.         // set the target position to where we are at the start
    26.         targetPosition = transform.position;
    27.         // set out move toogle to false.
    28.         isMoving = false;
    29.     }  
    30.  
    31.     /// <summary>
    32.     /// Detect the player input every frame
    33.     /// </summary>
    34.     void Update()
    35.     {
    36.         // if the player clicked on the screen, find out where
    37.         if(Input.GetMouseButton(LEFT_MOUSE_BUTTON))
    38.             SetTargetPosition();
    39.        
    40.         // if we are still moving, then move the player
    41.         if(isMoving)
    42.             MovePlayer();
    43.     }
    44.    
    45.     /// <summary>
    46.     /// Sets the target position we will travel too.
    47.     /// </summary>
    48.     void SetTargetPosition()
    49.     {
    50.         Plane plane = new Plane(Vector3.up, transform.position);
    51.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    52.         float point = 0f;
    53.        
    54.         if(plane.Raycast(ray, out point))
    55.             targetPosition = ray.GetPoint(point);
    56.        
    57.         // set the ship to move
    58.         isMoving = true;
    59.     }
    60.    
    61.     /// <summary>
    62.     /// Moves the player in the right direction and also rotates them to look at the target position.
    63.     /// When the player gets to the target position, stop them from moving.
    64.     /// </summary>
    65.     void MovePlayer()
    66.     {
    67.         transform.LookAt(targetPosition);          
    68.         transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
    69.        
    70.         // if we are at the target position, then stop moving
    71.         if(transform.position == targetPosition)
    72.             isMoving = false;
    73.  
    74.         Debug.DrawLine(transform.position, targetPosition, Color.red);
    75.     }
    76. }