i need to move character in the camera view direction in Unity3d, in game i have a 3rd person camera
this is moving script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movePlayer : MonoBehaviour
{
private Rigidbody player;
public float speed = 5f;
private Vector3 moveVector;
void Awake()
{
player = GetComponent<Rigidbody>();
}
void Update()
{
moveVector.x = Input.GetAxis("Horizontal");
moveVector.z = Input.GetAxis("Vertical");
player.MovePosition(player.position + moveVector * speed * Time.deltaTime);
}
}
this is camera script
using UnityEngine;
public class FPCameraController : MonoBehaviour
{
[SerializeField] private float _sensivity;
[SerializeField] private Transform _player;
[SerializeField] private float _verticalLover;
[SerializeField] private float _verticalUpper;
private float _currentVerticalAngle;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
var vertical = -Input.GetAxis("Mouse Y") * _sensivity * Time.deltaTime;
var horizontal = Input.GetAxis("Mouse X") * _sensivity * Time.deltaTime;
_currentVerticalAngle = Mathf.Clamp(_currentVerticalAngle + vertical, _verticalUpper, _verticalLover);
transform.localRotation = Quaternion.Euler(_currentVerticalAngle, 0, 0);
_player.Rotate(0, horizontal, 0);
}
}
i tried a lot of material about it, switched many of moving and cameras scripts but nothing works
New contributor
Gamer’s creed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.