- Home /
Object snaps from point A to B
I'm trying to move my object from point A to B without it snapping.
I have used:
Vector3.MoveToward
and
Vector3.Lerp
Maybe I am not using it right?
I also tried a While loop
while (HookShot == true)
{
if (HookShot == false)
{
break;
}
HookChild.Translate(Vector3.MoveTowards(Player.transform.position,HookHit.point,Mathf.Infinity));
}
Which basically caused a crash.
void Update ()
{
if (GameObject.FindGameObjectsWithTag("Hook").Length != 1)
{
HookChild = Instantiate(Hook,new Vector3(Player.position.x,Player.position.y,Player.position.z + 2)
,Quaternion.identity) as Transform;
HookChild.transform.parent = Player;
HookShot = false;
}
if(GameObject.FindGameObjectsWithTag("Rope").Length != 1)
{
RopeChild = Instantiate(Rope,new Vector3(HookChild.position.x, HookChild.position.y,HookChild.position.z)
,Quaternion.identity) as Transform;
RopeChild.transform.parent = Player;
RopeChild.localScale = new Vector3 (0.5f,2.0f,0.5f);
}
//-----------------------------------------------------------------------------------//
Ray HookRay = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(HookRay.origin,HookRay.direction,Color.green);
Vector3 curHookPos = HookChild.position;
Vector3 newHookPos = HookChild.position;
if (Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(HookRay, out HookHit, 50.0f) == true)
{
HookShot = true;
Debug.DrawRay(HookRay.origin,HookRay.direction, Color.red);
Debug.Log("Hook was hit");
newHookPos = Vector3.Lerp(curHookPos,HookHit.point,speed * Time.deltaTime);
RopeChild.localRotation = Quaternion.LookRotation(Player.transform.forward,HookHit.point);
RopeChild.localScale += ropeMoveTowards * Time.deltaTime;
}
}
else if (Input.GetMouseButtonUp(0))
{
newHookPos = curHookPos;
RopeChild.rotation = Quaternion.identity;
RopeChild.localScale = new Vector3 (0.5f,2.0f,0.5f);
HookShot = false;
}
}
Ignore the Rope stuff, I'm just messing around with that.
I don't know why $$anonymous$$oveToward is wrong? Just simple:
public float moveSpeed; //the speed of object you wanna move
void Update()
{
transform.position =Vector3.$$anonymous$$oveToward(transform.position,target.transform.position,moveSpeed*Time.deltaTime);
}
Target is a GameObject type.
Answer by RPGstandard · Jul 31, 2013 at 01:26 AM
Is this what your looking for?
http://docs.unity3d.com/Documentation/ScriptReference/Handles.FreeMoveHandle.html
Can you convert that to C#? It won't change it on the Docs.
Typically if it doesnt change inside the Docs than it should work in either language.
none of the code in the linked Doc is Javascript or C# specific.
// Create a simple move handle (Twice as big) on the
// target object that lets you freely move the Object
// Without having the "$$anonymous$$ove" button selected
@CustomEditor (Free$$anonymous$$ove)
class Free$$anonymous$$oveHandleJS extends Editor {
function OnSceneGUI () {
target.pos = Handles.Free$$anonymous$$oveHandle(target.pos,
Quaternion.identity,
2.0,
Vector3.zero,
Handles.DrawRectangle);
if (GUI.changed)
EditorUtility.SetDirty (target);
}
}
@script ExecuteInEdit$$anonymous$$ode()
var pos : Vector3 = Vector3(0,0,0);
function Update () {
transform.position = pos;
}
That is Javascript right?
Your answer
Follow this Question
Related Questions
moving an object from point to point using force 1 Answer
Does the speed of the object depends on target distance while using vector3.movetowards() 0 Answers
How do I move a Cube transform from one position to another smoothly? 1 Answer
MoveTowards won't translate on the y axis and lerp leaves character jittering in mid-air 0 Answers
Moving an object constantly between two points with a time delay inbetween 2 Answers