Player rotating and moving towards camera direction in 3rd person view.
So recently, I've started with unity. I am kind of new to how 3d scripts work, and I am not able to write my code without someones help. I've been searching a lot on the unity forums, and on youtube but found no answer to my problem. So basically I have this code for my camera:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraFollow : MonoBehaviour {
public float CameraMoveSpeed = 120.0f;
public GameObject CameraFollowObj;
Vector3 followPOS;
public float clampAngle = 80.0f;
public float inputSensitivity = 150.0f;
public GameObject CameraObj;
public GameObject PlayerObj;
public float camDistanceXToPlayer;
public float camDistanceYToPlayer;
public float camDistanceZToPlayer;
public float mouseX;
public float mouseY;
public float finalInputX;
public float finalInputZ;
public float smoothx;
public float smoothy;
private float rotY = 0.0f;
private float rotX = 0.0f;
void Start()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
float inputX = Input.GetAxis ("RightStickHorizontal");
float inputZ = Input.GetAxis ("RightStickVertical");
mouseX = Input.GetAxis ("Mouse X");
mouseY = Input.GetAxis ("Mouse Y");
finalInputX = inputX + mouseX;
finalInputZ = inputZ + mouseY;
rotY += finalInputX * inputSensitivity * Time.deltaTime;
rotX += finalInputZ * inputSensitivity * Time.deltaTime;
rotX = Mathf.Clamp (rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler (rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
void LateUpdate()
{
CameraUpdater ();
}
void CameraUpdater ()
{
Transform target = CameraFollowObj.transform;
float step = CameraMoveSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, target.position, step);
}
}
It is an orbit camera, that rotates around the CameraBase object that is inside the player, and the camera rotates with mouse movement. It also has a camera collision script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraCollision : MonoBehaviour {
public float minDistance = 1.0f;
public float maxDistance = 4.0f;
public float smooth = 10.0f;
Vector3 dollyDir;
public Vector3 dollyDirAdjusted;
public float distance;
void Awake () {
dollyDir = transform.localPosition.normalized;
distance = transform.localPosition.magnitude;
}
void Update () {
Vector3 desiredCameraPos = transform.parent.TransformPoint (dollyDir * maxDistance);
RaycastHit hit;
if (Physics.Linecast (transform.parent.position, desiredCameraPos, out hit)) {
distance = Mathf.Clamp ((hit.distance * 0.9f), minDistance, maxDistance);
} else {
distance = maxDistance;
}
transform.localPosition = Vector3.Lerp (transform.localPosition, dollyDir * distance, Time.deltaTime * smooth);
}
}
My question is, could someone help me make a script that makes the player go:
Towards the camera direction on the X and Z axis, as I press W.
Away from the camera direction on the X and Z axis as I press S.
And as I press A and S, I want the player to move Horizontally on the X and Z axis, depending on the direction of the camera.
Could someone that is experienced provide such coding for me, please? :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612140657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Character movement 0 Answers
(Newbie's First Project) Object doesn't stop moving until long after I let go of the button 1 Answer
Regarding transform.position in the roll a ball tutorial 0 Answers
How To Move My Player With Sin And Cos Functions 1 Answer
How can I remove movement or camera rotation in one direction with a trigger 0 Answers