Hello everyone,
So, I am practicing on scripting and I am trying to create Third Person Camera and Third Person Character Control scripts. I am not a programmer, so it is actually a hard task for me. So, I am getting a little bit of help from this series:
I almost exactly copied the script written in the video, just made little adjustments on the names of stuff. What I want to achieve is a simple TPS setting; character moves in the direction of the camera. But when I insert the code that connects movement to the camera direction, my character doesn’t move. Without connecting them to each other, everything works just fine; the only problem is that character doesn’t rotate according to camera.
My movement script is here;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float walkSpeed = 5f;
public float runSpeed = 10f;
public float turnSmoothTime = 0.12f;
float turnSmoothVelocity;
public float speedSmoothTime;
float speedSmoothVelocity;
float currentSpeed;
Transform cameraT;
GameObject Player;
Animator animator;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
cameraT = Camera.main.transform;
}
// Update is called once per frame
void Update()
{
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector2 inputDir = input.normalized;
if (inputDir != Vector2.zero)
{
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
}
bool running = Input.GetKey(KeyCode.LeftShift);
float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
transform.Translate(transform.forward * currentSpeed * Time.deltaTime, Space.World);
float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;
animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
}
}
My camera script is here;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera2 : MonoBehaviour
{
float yaw;
float pitch;
public float mouseSensitivity = 10f;
public Transform target;
public float dstFromTarget = 2;
public Vector2 pitchMinMax = new Vector2(-40, 90);
public float rotationSmoothTime = 1.2f;
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
private void LateUpdate()
{
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
transform.eulerAngles = currentRotation;
transform.position = target.position - transform.forward * dstFromTarget;
}
}
What should i do to make my character move, but accordingly to rotation of the camera?