0
votes

I have implemented a click 2 move script and i'm trying to use a method that already gets a Vector3 of where I clicked and moves the player via CharacterController to it.

is there a way to take the transforms current position, get the values from GetAxis while the keys are down and calculate its new position. when the vertical/horizontal controls are release the Vector3 would be assigned the transforms current position so it will stop moving

the is what is used to move to the position.

void MoveToPosition()
{
    if (Vector3.Distance(transform.position, position) > 1)
    {
        Quaternion newRotation = Quaternion.LookRotation(position - transform.position);
        newRotation.x = 0f;
        newRotation.z = 0f;
        transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
        controller.SimpleMove(transform.forward * speed);
        animation.CrossFade(run.name);
    }
    else
    {
        animation.CrossFade(idle.name);
    }
}

position is assigned from a raycast so I would like to use the players current position, what ever the calculations are required with getaxis and adjust accordingly

1
Do I understand you want to click, click again, and move from the first click pos to the second?theodox
Do you mean you want the player to keep moving towards the mouse while the left mouse button is down? It's unclear what exactly you're asking...Steven Mills

1 Answers

1
votes

Sure, back when games where done without a engine that was the simplest way to calculate collision detection,

but you will have to take in to account a lot of variables and how they change. basically if you wanna know where the player is going to be you have to use the simplest of math, addition. calculating where the player is going to be you have to take the vector and add the velocity.

so position+(Vector3.forward) in a update function would make it increase Vector3(0,0,n) with each frame.

void Update() {
   Vector3 newpos = transform.position + Vector3.forward;
   transform.position = newpos;
}

In short that is what the code would have to do, if we use Vector3.forward, but you will most likely use your own variables such as

Vector3 moveForward = new Vector3(0,0,Input.GetAxis("Horizontal"));
moveForward += transform.position;

if that is called after the move has been performed that frame, you will get the position on the forward axix ( you'll also have to add from side to side and up and down i guess ) but its the position of the player next frame, so you can check if a wall is there and if so dont allow player to move forward next frame, which is old school collision detection.

I hope that gave some clarity.