Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help, I need a game object to move towards another that you select in the game

Discussion in 'Scripting' started by casti2308, May 17, 2019.

  1. casti2308

    casti2308

    Joined:
    Dec 8, 2017
    Posts:
    3
    It's like a click to move, you have a ship and you select a planet and move towards it, moving from planet to planet.
    I have used a raycast to that the selected one with the mouse is the one to which the ship is directed, but it does not work properly , I have also tried a navmesh, but the same thing happens to me, some advice?
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Can’t you use just the clicked and the ship position with a vector3 leap and look towards or something similar, can you post your code you have tried?
     
    casti2308 likes this.
  3. casti2308

    casti2308

    Joined:
    Dec 8, 2017
    Posts:
    3
    Thank, sure here is the code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Carbine : MonoBehaviour
    5. {
    6.     public GameObject cam;//maincamera
    7.     public GameObject camPos;//empty object associated with the camera to track  
    8.     public GameObject targetPos;//worcks like a objetive market
    9.     public GameObject eyePos;//object that traces a straight line with the objective to identify if there is an obstacle
    10.     public GameObject ostaculePos;//worcks like a obstacule market
    11.  
    12.     Vector3 newpos;
    13.     bool moving;
    14.     void start()
    15.     {
    16.         moving = false;
    17.     }
    18.     void update()
    19.     {
    20.         if (Imput.GetMauseButtonDown(0))
    21.         {
    22.             Raycasthit hitplanet;
    23.             Raycasthit hitsun;
    24.  
    25.             Ray ray = cam.getcomponent<camera>().ScreenPointToray(Input.mausePosition);
    26.             if (Physics.Raycast(ray, out hitplanet)) {
    27.                 if (hitplanet.colaider.comparetag("planet")){
    28.                     newpos = hitplanet.point;
    29.                     targetPos.transform.position = newpos;
    30.                     camPos.transform.Setparent(null);
    31.                     transform.LookAt(new Vector3(newpos.x, transform.position.y, newpos.z));
    32.                     camPos.transform.Setparent(transform);
    33.                     if (Physiscs.Raycast (eyePos.transform.position, newpos - eyePos.transform.position, out hitsun, Vector3.Distance(transform.position, newpos)))
    34.                     {
    35.                         if (hitsun.colaider.comparetag("sun"))
    36.                         {
    37.                             ostaculePos.transform.position = hitsun.point;
    38.                             Debug.log("Sun in the middle of the path");
    39.                         }
    40.                         else
    41.                         {
    42.                             moving = true;
    43.                         }
    44.                     }
    45.                     else
    46.                     {
    47.                         moving = true;
    48.                     }
    49.  
    50.                 }
    51.             }
    52.         }
    53.         if (moving == true)
    54.         {
    55.             transform.translate(new Vector3(0, 0, 100f));
    56.             if(Vector3.Distance(transform.position, newpos)<50f)
    57.             {
    58.                 moving = false;
    59.             }
    60.         }
    61.     }
    62.     cam.transform.position = Vector3.lerp (cam.transform.position,camPos.transform.position,0.1f);
    63. }
    64.  
     
  4. casti2308

    casti2308

    Joined:
    Dec 8, 2017
    Posts:
    3
    I have more codes, translation and rotation of the planets, but first I want to solve this