Hello
I’ve a problem with mu GameObjects. When i right click destination they go there but when there’re close they suddenly jump on clicked place. What’s more if i click on highland they dont “climb” up - they move trough the grounf and it seems that half is above ground lever and half under… Thanks for help
BasicUnitMovement.cs
using UnityEngine;
using System.Collections;
public class BasicUnitMovement : MonoBehaviour
{
public float moveSpeed = 1.0f;
public float goalRadius = 0.1f;
private Vector3 goal;
void Start()
{
goal = transform.position;
}
public void MoveOrder(Vector3 newGoal)
{
goal = newGoal;
}
void Update()
{
//Move towards our goal
transform.position += (goal - transform.position).normalized*moveSpeed*Time.deltaTime;
foreach(Collider obj in Physics.OverlapSphere(goal,goalRadius))
{
if(obj.gameObject == gameObject)
{
transform.position = goal;
}
}
}
}
MoveSelectedUnitsOnRightClick
using UnityEngine;
using System.Collections;
public class MoveSelectedUnitsOnRightClick : MonoBehaviour
{
private UnitManager unitManager;
void Start()
{
GameObject unitManagerObject = GameObject.FindGameObjectWithTag("PlayerUnitManager");
unitManager = unitManagerObject.GetComponent<UnitManager>();
}
void RightClicked(Vector3 clickPosition)
{
foreach(GameObject unit in unitManager.GetSelectedUnits())
{
unit.SendMessage("MoveOrder",clickPosition);
}
}
}
Problem 1: Male goalRadius smaller so that it does not jump there. Problem 2: Your object doesn’t detect collision with the ground and sounds like it is moving to centre of object. Create system to check for collision between unit and terrain. For second issue, if the unit is moving so that it’s position is the centre of the object, create y offset so that it goes to the bottom of the unit.
Forget to mention, if you use Unity terrain you can easily adjust your character position with Terrain.SampleHeight()
I you want objects to collide with other objects and be dynamically moving around then:
Make sure each dynamic (moving) object has a Rigidbody component.
Instead of changing an objects position you should apply a force to the Rigidbody of the object in the direction of the goal position.
When I use the AddForce() function I usually pass it ForceMode.VelocityChange as it’s much easier to work with than calculating what forces you need to push objects of differing mass.
One last tip, when working with the physics engine it is a good idea to put all your physics code in the FixedUpdate() method instead of the Update method in your scripts.
Apparently this is issue with casting OverlapSphere.Look here. You could replace with
if ((Vector3.Distance(transform.position, goal) < goalRadius))
{
transform.position = goal;
}
but I do not know if this work for your implementation.
With centre of object problem, this is because of the object pivot. If you use default object like Sphere it will be in the centre of the object so when you move it will go to centre of object because it’s transform.position is there. To solve, you can move the pivot or shoot a ray down from centre of object and check distance to floor, then offset y position based on distance to floor.
To use Terrain.SampleHeight() you can do like this
If I use just that code (with small modification so that is click-to-move) it work how it should. I can upload video later today (8+ hour) when I get home if you want me to. Make sure any script that affect that object don’t interfere with your movement.
Don’t see anything there that should cause problem. If you use rigidbody there are things that cause similar problem but from video I do not see you use it. So I don’t know what can be problem, sorry.