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 /
  • Help Room /
avatar image
0
Question by Chocolade · Sep 26, 2017 at 12:42 PM · c#scripting problemscript.

Why the object using Vector3.MoveTowards is not moving down ?

I'm using this script on a object to move the object from one position to a target position and when the object is above the target i want the object to move directly down.

But instead what it does not is when the object is above the other object the target then he just rotating in the air above the target. I can't figure out why it's not moving down and why the object keep rotating above the target.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FlyToOverTerrain : MonoBehaviour
 {
     public Transform target;
     public float desiredHeight = 10f;
 
     public float flightSmoothTime = 10f;
     public float maxFlightspeed = 10f;
     public float flightAcceleration = 1f;
 
     public float levelingSmoothTime = 0.5f;
     public float maxLevelingSpeed = 10000f;
     public float levelingAcceleration = 2f;
     
     private Vector3 flightVelocity = Vector3.zero;
     private float heightVelocity = 0f;
 
     private RaycastHit hit;
 
     private void Start()
     {
 
     }
 
     private void LateUpdate()
     {
         Vector3 position = transform.position;
         float currentHeight = position.y;
         if (target && flightAcceleration > float.Epsilon)
         {
             position = Vector3.SmoothDamp(position, target.position, ref flightVelocity, flightSmoothTime / flightAcceleration, maxFlightspeed, flightAcceleration * Time.deltaTime);
         }
 
         if (levelingAcceleration > float.Epsilon)
         {
             float targetHeight = Terrain.activeTerrain.SampleHeight(position) + desiredHeight;
 
             position.y = Mathf.SmoothDamp(currentHeight, targetHeight, ref heightVelocity, levelingSmoothTime / levelingAcceleration, maxLevelingSpeed, levelingAcceleration * Time.deltaTime);
         }
 
         transform.position = position;
 
         if (Physics.Raycast(transform.position, Vector3.down, out hit))
         {
             if (hit.transform == target)
             {
                 transform.position = Vector3.MoveTowards(transform.position, target.position, 3 * Time.deltaTime);
             }
         }
         else
         {
             transform.position = position;
         }
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MaxGuernseyIII · Sep 26, 2017 at 05:27 PM

It's because the only condition for setting position.y is whether or not there is a leveling acceleration. I added a flag to determine whether or not you are over the target and use that to control the "hovering" part of your code...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FlyToOverTerrain : MonoBehaviour
 {
   public Transform target;
   public float desiredHeight = 10f;
 
   public float flightSmoothTime = 10f;
   public float maxFlightspeed = 10f;
   public float flightAcceleration = 1f;
 
   public float levelingSmoothTime = 0.5f;
   public float maxLevelingSpeed = 10000f;
   public float levelingAcceleration = 2f;
 
   private Vector3 flightVelocity = Vector3.zero;
   private float heightVelocity = 0f;
   private bool overTarget = false;
 
   private RaycastHit hit;
 
   private void Start()
   {
 
   }
 
   private void LateUpdate()
   {
     Vector3 position = transform.position;
     float currentHeight = position.y;
     if (target && flightAcceleration > float.Epsilon)
     {
       position = Vector3.SmoothDamp(position, target.position, ref flightVelocity, flightSmoothTime / flightAcceleration, maxFlightspeed, flightAcceleration * Time.deltaTime);
     }
 
     if (!overTarget && levelingAcceleration > float.Epsilon)
     {
       float targetHeight = Terrain.activeTerrain.SampleHeight(position) + desiredHeight;
 
       position.y = Mathf.SmoothDamp(currentHeight, targetHeight, ref heightVelocity, levelingSmoothTime / levelingAcceleration, maxLevelingSpeed, levelingAcceleration * Time.deltaTime);
     }
 
     transform.position = position;
 
     if (Physics.Raycast(transform.position, Vector3.down, out hit))
     {
       if (hit.transform == target)
       {
         overTarget = true;
         transform.position = Vector3.MoveTowards(transform.position, target.position, 3 * Time.deltaTime);
       }
     }
     else
     {
       transform.position = position;
     }
   }
 }

All that said, there's a state pattern in your problem. You want to hover over the terrain until you are over the target and then start lowering. You might consider refactoring accordingly.

Also the transform.position assignment at the end is superfluous. It already happened before you checked the raycast. :)

Comment
Add comment · Show 2 · Share
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
avatar image Chocolade · Sep 26, 2017 at 06:15 PM 0
Share

This is working great but i have some questions please.

The target is not the terrain but a cube as a platform. So now it's moving down on the target but too much down i want it to be on the target not inside or under. For example to be in height 1 on the target. So i need a variable or a way to set the height it will be stop down above the target.

But the idea is to land it on the target like a landing platform.

Second i removed this part:

 else
      {
        transform.position = position;
      }

Third i didn't understand what to do here:

"All that said, there's a state pattern in your problem. You want to hover over the terrain until you are over the target and then start lowering. You might consider refactoring accordingly."

The idea in the script is that the spaceship will move hover over the terrain using the SampleHeight and once it's reaching above the target just land on it. The target is not the terrain in this case. If you could show me please how or what to do.

Here is a screenshot showing the spaceship after landing on the cube(platform).

Landed

Thank you.

landing.jpg (221.8 kB)
avatar image MaxGuernseyIII Chocolade · Sep 26, 2017 at 07:05 PM 1
Share

The state pattern thing is design advice. You can follow the thread if/when you are interested. I gave you a link to a description of the state pattern on wikipedia as a starting point. You can safely ignore it if you like.

If you want it to stop at a certain point relative to the terrain, you'll need to change the point toward which you are moving.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FlyToOverTerrain : $$anonymous$$onoBehaviour
 {
   public Transform target;
   public float desiredHeight = 10f;
 
   public float flightSmoothTime = 10f;
   public float maxFlightspeed = 10f;
   public float flightAcceleration = 1f;
 
   public float levelingSmoothTime = 0.5f;
   public float maxLevelingSpeed = 10000f;
   public float levelingAcceleration = 2f;
   public float landingHeight = 1f;
 
   private Vector3 flightVelocity = Vector3.zero;
   private float heightVelocity = 0f;
   private bool overTarget = false;
 
   private RaycastHit hit;
 
   private void LateUpdate()
   {
     Vector3 position = transform.position;
     float currentHeight = position.y;
     if (target && flightAcceleration > float.Epsilon)
     {
       position = Vector3.SmoothDamp(position, target.position, ref flightVelocity, flightSmoothTime / flightAcceleration, maxFlightspeed, flightAcceleration * Time.deltaTime);
     }
 
     if (!overTarget && levelingAcceleration > float.Epsilon)
     {
       float targetHeight = GetTargetHeight(position, desiredHeight);
 
       position.y = $$anonymous$$athf.SmoothDamp(currentHeight, targetHeight, ref heightVelocity, levelingSmoothTime / levelingAcceleration, maxLevelingSpeed, levelingAcceleration * Time.deltaTime);
     }
 
     transform.position = position;
 
     if (Physics.Raycast(transform.position, Vector3.down, out hit))
     {
       if (hit.transform == target)
       {
         overTarget = true;
         var targetPosition = target.position;
         targetPosition.y = GetTargetHeight(target.position, landingHeight);
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, 3 * Time.deltaTime);
       }
     }
     else
     {
       transform.position = position;
     }
   }
 
   private float GetTargetHeight(Vector3 position, float desiredHeight)
   {
     return Terrain.activeTerrain.SampleHeight(position) + desiredHeight;
   }
 }

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

412 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I create material with script but it does not render right 0 Answers

Creating Splines from empties in script 0 Answers

How can i create List of maps from each Map class ? 0 Answers

How can i rotate all the child objects together at the same time ? 1 Answer

How can i give another name/number to the created Plane object name ? 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