0
\$\begingroup\$

I'm working on a lock on function and so far I can lock on to the nearest enemy, but my player can't move around the enemy, i can only move towards it. I have my project setup so that the camera is always at a fixed position/rotation, and the player moves independently of the camera.

Here's my move function:

public void Move(float h, float v) {
    if (!lockPlayerMovement) {
        Speed = 0;
        MoveDirection.y -= Gravity * Time.deltaTime;
        if (Mathf.Abs(v) > ThumbStickDeadZone || (Mathf.Abs(h) > ThumbStickDeadZone)) {
            var lookRotation = new Vector3();
            lookRotation.Set(h, 0, v);
            lookRotation = lookRotation.normalized;

            var rotation = Quaternion.LookRotation(lookRotation, Vector3.up);
            if (lookRotation.magnitude > ThumbStickDeadZone) {
                Speed = RunSpeed;
                transform.rotation = Quaternion.Slerp(Transform.rotation, rotation, Time.deltaTime * TurnSpeed);
            }
        }
        if (Controller.isGrounded) {
            MoveDirection = Transform.TransformDirection(Vector3.forward).normalized;
            MoveDirection *= Speed * Time.deltaTime;
            //if (Input.GetButton("Fire1"))
            //      MovementProperties.MoveDirection.y = JumpSpeed;
        }
        Controller.Move(MoveDirection);
    }
}

Lock On Function:

void Update () {
    playerPosition = transform;

    if (enemyLockedOn && enemyTarget != null) {
        transform.LookAt(enemyTarget.transform);
    }
}



void ScanForEnemies() {
    int numOfEnemies = 0;

    float distanceFromPlayer;
    GameObject closestEnemy;

    List<GameObject> enemies = new List<GameObject>();

    Collider[] cols = Physics.OverlapSphere(transform.position, radius);

    foreach (Collider col in cols) {
        if (col && col.tag == "Enemy") {
            ++numOfEnemies;
            enemies.Add(col.gameObject);
            //print(Vector3.Distance(playerPosition.position, col.transform.position));
        }
    }

    if (enemies.Count > 0) {
        distanceFromPlayer = Vector3.Distance(playerPosition.position, enemies[0].transform.position);
        closestEnemy = enemies[0];
        for (int i = 0; i < enemies.Count; i++) {
            if (Vector3.Distance(playerPosition.position, enemies[i].transform.position) < distanceFromPlayer) {
                distanceFromPlayer = Vector3.Distance(playerPosition.position, enemies[i].transform.position);
                closestEnemy = enemies[i];
            }
        }

        /* Set closest enemy as the player target and lock player to that enemy (while in range -- or scan pressed again) */
        enemyTarget = closestEnemy;

        if (enemyTarget != null) enemyLockedOn = true;

        print("Closest Enemy : " + closestEnemy + " | Distance From Player : " + distanceFromPlayer);
        print("Number of nearest enemies : " + numOfEnemies);

    }
}
\$\endgroup\$
1
\$\begingroup\$

There's quite some code there, and some variables which are not explained, but I think the culprit is:

MoveDirection = Transform.TransformDirection(Vector3.forward).normalized;

This line makes the MoveDirection variable to move towards the +Z axis. Assuming that the code that handles this movement is changing the player's localPosition, this means that the player will be moving towards the direction it is pointing, in this case the locked enemy.

To move backwards, you can either use -Vector3.forward or Vector3.back, but I'm not sure what would be the best way to implement that in your project, as I can only see part of it.

Or the alternative is this line:

MoveDirection *= Speed * Time.deltaTime;

If you set speed to a negative values (as in, going backwards) this line will automatically make the player move backwards.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.