Hi,
Please help me disable the rotation of the player when moving to the sides and back. I'm looking to disable the function that makes the player face the direction it's moving towards and instead side strafe and walk backward when I hold down right mouse button. I'm using direct movement and relative to camera.
I'm using the script below to lock the player to face a target on button hold, and the normal AC movement rotation prevents him from moving in any direction he is not facing.
I tried freezing the Y rotation in the player's rigidbody, but it can still rotate as usual.
Im using:
Unity v. 2020.3.28f1
AC v. 1.76.1
Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AC;
public class LockTarget : MonoBehaviour
{
void Update()
{
bool defensiveStance = GlobalVariables.GetBooleanValue(5); // Replace 1 with the ID of your Global Variable
if (defensiveStance)
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
if (enemies.Length > 0)
{
Transform closestEnemy = GetClosestEnemy(enemies);
transform.LookAt(closestEnemy);
}
}
}
Transform GetClosestEnemy(GameObject[] enemies)
{
Transform closestEnemy = null;
float closestDistance = Mathf.Infinity;
Vector3 currentPosition = transform.position;
foreach (GameObject enemy in enemies)
{
float distance = Vector3.Distance(enemy.transform.position, currentPosition);
if (distance < closestDistance)
{
closestDistance = distance;
closestEnemy = enemy.transform;
}
}
return closestEnemy;
}
}
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Welcome to the community, @martinlilius.
The Direct movement approach requires that the Player face the direction they're moving - you'd need to handle the movement manually to get this behaviour.
This function should allow for this:
You'd need to make sure that your "Movement method" is set to "None", however. This can be done through script at the point you need this movement style to work:
Hi Chris, Thanks for your response!
I got the movement method change to work, but I guess I'm yet too inexperienced to know how to use the movement script. The player isn't moving. What could I be doing wrong?
I tried pasting it below my existing code, as well as inside the if(defensiveStance).
I also tried to add it to its own script witch I attached to the player prefab. When attached as component, it doesn't have the enable/disable tick, so something seems wrong there.
Update:
I got it working with this script below. The only issue is that the Player continues to move in the direction it was moving with AC movement, if this new movement is activated when he is still in motion.
Is there any way to prevent him from moving instantly as the new movement is activated? Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AC;
public class StrafeMovement : MonoBehaviour
{
public float speed = 5f;
}
Your script replaces the AC movement code - if you want to move your character manually, you'll need to set their "Motion control" field to "Manual".
This can be done through script with:
To have AC stop moving the character, call:
Thanks! "Halt solved that issue"
I set the "MovementMethod.None" first, witch seems to work - the new movement script took over. Do I still need to Set motionControl.manual?
Or is the motionControl.Manual a better choice, so I can remove MovementMethod.None?
If you use my script, which moves the player using built-in commands, use MovementMethod.None.
If you're using your own script, with affects the position by directly manipulating the transform, use MotionControl.Manual.
Ok, thanks for your help!
Hi again Chris,
Sorry but I'm back to my previous question about how to use your function. Where do I put it inside my first script, or should I put it in a new script?
And does it only turn off the rotation, or focus on a target? My initial script focuses on the nearest enemy, do I need to remove that part?
Thanks again.
With v1.77.3, I added a DirectMovementTargetLock property that you can set in the Player component - if set to a Transform, then the Player character will face it at all times when under Direct control.
Going back to your original script, try replacing:
with: