Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by benydayag · Oct 05, 2016 at 09:55 AM · c#scripting problem

How can i move objects around the terrain area and keep the objects to move only in the terrain area ?

The main idea is to make that when the thirdpersoncontroller user will look up to the sky he will see many spaceships moving around like in the movies when you see spaceships moving around.

So the objects will not move out of the terrain area. The problem is that my objects are spaceships so they are in the air. The secon problem is that each time i'm running my game the spaceships starting position is changing. My script now make that the spaceships will be randomaly at new starting positions each time starting the game. Also at some height above the terrain.

Now what i want to do is that when i'm running the game each object(spaceship) will start moving forward until the spaceship get to the terrain border then it will lerp/turn back and move all the way to the other side of the terrain border and then will lerp/turn back and again nonstop will keep moving between the terrain borders.

Today i created a new script of waypoints so i can make the spaceships move between two spheres. But i want each ship to move between the terrain borders.

This is the script that create clones of the spaceships: The script was made in original for Spheres but i changed it for my objects the spaceships:

 using System;
 using UnityEngine;
 using Random = UnityEngine.Random;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SphereBuilder : MonoBehaviour
 {
     public GameObject SpaceShip;
     private List<GameObject> ships = new List<GameObject> ();
 
     // for tracking properties change
     private Vector3 _extents;
     private int _sphereCount;
     private float _sphereSize;
 
     /// <summary>
     ///     How far to place spheres randomly.
     /// </summary>
     public Vector3 Extents;
 
     /// <summary>
     ///     How many spheres wanted.
     /// </summary>
     public int SphereCount;
     public float SphereSize;
 
     private void OnValidate()
     {
         // prevent wrong values to be entered
         Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
         SphereCount = Mathf.Max(0, SphereCount);
         SphereSize = Mathf.Max(0.0f, SphereSize);
     }
 
     private void Reset()
     {
         Extents = new Vector3(250.0f, 20.0f, 250.0f);
         SphereCount = 100;
         SphereSize = 20.0f;
     }
 
     private void Update()
     {
         UpdateSpheres();
     }
 
     private void UpdateSpheres()
     {
         if (Extents == _extents && SphereCount == _sphereCount && Mathf.Approximately(SphereSize, _sphereSize))
             return;
 
         // cleanup
         var spheres = GameObject.FindGameObjectsWithTag("Sphere");
         foreach (var t in spheres)
         {
             if (Application.isEditor)
             {
                 DestroyImmediate(t);
             }
             else
             {
                 Destroy(t);
             }
         }
 
         var withTag = GameObject.FindWithTag("Terrain");
         if (withTag == null)
             throw new InvalidOperationException("Terrain not found");
 
         for (var i = 0; i < SphereCount; i++)
         {
             var o = Instantiate(SpaceShip);
             o.tag = "Sphere";
             o.transform.localScale = new Vector3(SphereSize, SphereSize, SphereSize);
 
             // get random position
             var x = Random.Range(-Extents.x, Extents.x);
             var y = Extents.y; // sphere altitude relative to terrain below
             var z = Random.Range(-Extents.z, Extents.z);
 
             // now send a ray down terrain to adjust Y according terrain below
             var height = 10000.0f; // should be higher than highest terrain altitude
             var origin = new Vector3(x, height, z);
             var ray = new Ray(origin, Vector3.down);
             RaycastHit hit;
             var maxDistance = 20000.0f;
             var nameToLayer = LayerMask.NameToLayer("Terrain");
             var layerMask = 1 << nameToLayer;
             if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
             {
                 var distance = hit.distance;
                 y = height - distance + y; // adjust
             }
             else
             {
                 Debug.LogWarning("Terrain not hit, using default height !");
             }
 
             // place !
             o.transform.position = new Vector3(x, y, z);
         }
 
         _extents = Extents;
         _sphereCount = SphereCount;
         _sphereSize = SphereSize;
     }
 }


And this script is the waypoints:

 using UnityEngine;
 using System.Collections;
 
 public class WayPoints : MonoBehaviour {
 
     public Transform[] waypoints;
     public float duration = 1.0F;
     private Vector3 startPoint;
     private Vector3 endPoint;
     private float startTime;
     private int targetwaypoint;
 
     // Use this for initialization
     void Start () {
 
         startPoint = transform.position;
         startTime = Time.time;
 
         if (waypoints.Length <= 0)
             enabled = false;
 
         targetwaypoint = 0;
         endPoint = waypoints [targetwaypoint].position;
     }
 
     // Update is called once per frame
     void Update () {
 
         var i = (Time.time - startTime) / duration;
         transform.position = Vector3.Lerp (startPoint, endPoint, i);
         if (i >= 1) 
         {
             startTime = Time.time;
             targetwaypoint ++;
             targetwaypoint = targetwaypoint % waypoints.Length;
             startPoint = endPoint;
             endPoint = waypoints [targetwaypoint].position;
         }
 
 
     }
 }


And this is a screenshot showing the game in running mode you can see the spaceships and in the Hierarch the spaceships clones i want to move.

Screenshot

ss18.jpg (445.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

script with individual function for each gameObject or public script variable to solve inventory system problem 1 Answer

The vision of a camera (displayed as a 3D Cone) needs to rotate towards the player 1 Answer

Using AirBrakes on mobile device with Standard Assets AircraftController 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges