- Home /
How to make 3rd person stafe around target?
After a LOT of time. I was able to move the character forward towards the direction of the mouse. But, when i press other directional buttons (right, left, back) the characters turns to that direction and moves forward in that direction. But, I want to be able lock on to a target and strafe and walk backwards around that but only move forwards in the direction of mouse. ( witcher 3 combat, movement style) IDK how to did this because i am beginner and i did all this from a tutorial. Here is my Movement Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playercontrollerScript : MonoBehaviour
{
private Animator anim;
public float speed = 3.5f;
public float animspeed = 2.0f;
Vector2 input;
public Transform player;
public float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
public float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
Transform cameraT;
// Start is called before the first frame update
void Start()
{
anim = GetComponent();
cameraT = Camera.main.transform;
}
void Update()
{
anim.speed = animspeed;
// Move
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector2 inputDir = input.normalized;
if (inputDir != Vector2.zero)
{
float targetRoation= Mathf.Atan2 (inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRoation, ref turnSmoothVelocity, turnSmoothTime);
}
float targetSpeed = (speed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed ,targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
transform.Translate(transform.forward currentSpeed Time.deltaTime, Space.World);
float animationSpeedPercent = (3.5f) * inputDir.magnitude;
anim.SetFloat("VelY", animationSpeedPercent, speedSmoothTime, Time.deltaTime );
}
}
Any tips?
Your answer
Follow this Question
Related Questions
Remove keyboard controls from Third Person Controller?? 2 Answers
Why the ThirdPersonController is floating in the air when using Nav Mesh Agent ? 0 Answers
Third Person Platformer Controls 0 Answers
Third Person Flight? 0 Answers
i have to clamp a third person camera,I am trying to make a third person camera clamp. 0 Answers