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;
}
}
}
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. :)
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).
Thank you.
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
Follow this Question
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