Getting a new position from a current position

Hi sorry for the basic question but it’s giving me a headache :

So If I’ve got a Vector3 Handleoutposition, how do I set it to a new position say 10 units back from it’s original position :
I’ve tried

    Vector3 newpos = previousHandleOUT.HandleOutPosition + transform.forward * -10 ;

Which doesnt work ( also doesn’t bring up any errors)

In the past I’ve used a Gameobject and used transform.Translate to move it then got it’s position but that doesnt seem a very good way to do it.

Cheers

You’re gonna need to post more of your code so that we can understand because if you don’t have any compilation error then this line seems perfectly fine.

“Doesn’t work” is not an explanation. Say what does happen, and how that differs from what you wanted to have happen.

The code you posted is calculating a point that is 10 units away from HandleOutPosition in the direction opposite of transform.forward. Presumably either -transform.forward wasn’t the direction you wanted or there’s something wrong with how you’re using that point after you’ve calculated it.

ok when I say doesn’t work I mean the position does not change.

Debug.Log (" handle out pos before : " + previousHandleOUT.HandleOutPosition);
        Vector3 newpos = previousHandleOUT.HandleOutPosition + transform.forward * -10 ;

previousHandleOUT.HandleOutPosition = newpos;
        Debug.Log (" handle out pos after : " + previousHandleOUT.HandleOutPosition + " new pos = " + newpos);

In the console :
handle out pos before : (-566.3, 0.0, 406.4)
UnityEngine.Debug:Log(Object)

handle out pos after : (-566.3, 10.0, 406.4) new pos = (-566.3, 10.0, 406.4)
UnityEngine.Debug:Log(Object)

EDIT :

As a experiment I put the following code in update :

if (Input.GetKeyDown ("m")) {
            Vector3 newpos = positionercube.transform.position + transform.forward * 5;
            positionercube.transform.position = newpos;
        }

positionercube is a gameobject and is NOT parented to any other gameobject.
When I press m the cube moves DOWN by 5 units & the object is not rotated on any axis. Very strange.

That looks like it works fine, though.

No it’s not working because the handleout position after should be 10 units back from handle out pos before but its’ not moved.

EDIT : Just realised the Y position has moved up 10 units so it’s moved the position but not in the intended direction. :face_with_spiral_eyes: Just like with the little experiment I done.

Then it is because your object is facing the Y axis. Try with Vector3.forward instead of transform.forward to get the world coordinates.

if (Input.GetKeyDown ("m")) {
            Vector3 newpos = positionercube.transform.position + Vector3.forward * 5;
            positionercube.transform.position = newpos;
        }

No it still has the same effect. I think I need to use translate but not sure how to use it unless Ive got a reference to a object.s transform.

I cant do this

if (Input.GetKeyDown ("m")) {
            Vector3 newpos = positionercube.transform.position;
            newpos = transform.Translate (Vector3.forward * 5);
            positionercube.transform.position = newpos;
        }

I might have to go back to my old crappy method of placing a empty gameobject at the given position then using transform.translate to move it then getting the new position of the gameobject - must be a easier way.

So your code is already successfully moving the position by 10 units, just not in the direction you wanted.

All you have to do is figure out what direction you actually want, and how to express that direction in code.