Player rotates with camera(face same direction as the camera)

I have a 3rd person game where the camera moves around the player, the player is the axis and when i move the mouse the camera rotates around him. What i want to do is have my player turning with the camera, so I’ll always see the back of my character. What i tried to do is to add a EmptyGameObject(focoVisao) in the middle of the far clipping plane, and use transform.LookAt(focoVisao). It kinda works, but if I look up my character will rotate on the X axis and if I press W i can just move towards the sky. I want it to only rotate on the Y axis.

Here are the codes I’m using:

For the Player:

using UnityEngine;

class Move : MonoBehaviour // Sempre que houver a necessidade de mexer na cena tem de herdar a classe MonoBehavior
{
    public int vel = 5; // Se não colocar nada o programa entende a variavel como privada
	public Transform spawPoint;
    public Transform focoVisao;



    public void Update()
    {
        transform.Translate(vel * Time.deltaTime * Input.GetAxis("Horizontal"), 0, vel * Time.deltaTime * Input.GetAxis("Vertical")); // Time.deltaTima representa o tempo de resposta entre os frames
        transform.LookAt(focoVisao);
    }
	//colides with the rocks(die)
	public void OnCollisionEnter(Collision hit)
	{
		if(hit.gameObject.tag == "rochas")
		{
			transform.position = spawPoint.position;
		}

	}

    
}

And for the Camera:

using UnityEngine; 
using System.Collections; 

public class CameraScript : MonoBehaviour 
{ 
    public Transform target; 
    
    public float targetHeight = 1.7f; 
    public float distance = 5.0f;
    public float offsetFromWall = 0.1f;

    public float maxDistance = 20; 
    public float minDistance = .6f; 

    public float xSpeed = 200.0f; 
    public float ySpeed = 200.0f; 

    public int yMinLimit = -80; 
    public int yMaxLimit = 80; 

    public int zoomRate = 40; 

    public float rotationDampening = 3.0f; 
    public float zoomDampening = 5.0f; 
    
    public LayerMask collisionLayers = -1;

    private float xDeg = 0.0f; 
    private float yDeg = 0.0f; 
    private float currentDistance; 
    private float desiredDistance; 
    private float correctedDistance; 

    void Start () 
    { 
        Vector3 angles = transform.eulerAngles; 
        xDeg = angles.x; 
        yDeg = angles.y; 

        currentDistance = distance; 
        desiredDistance = distance; 
        correctedDistance = distance; 

        // Make the rigid body not change rotation 
        if (rigidbody) 
            rigidbody.freezeRotation = true; 
    } 
    
    /** 
     * Camera logic on LateUpdate to only update after all character movement logic has been handled. 
     */ 
    void LateUpdate () 
    { 
    	Vector3 vTargetOffset;
    	
       // Don't do anything if target is not defined 
        if (!target) 
            return; 

        // Mouse Controla Camera 
            xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f; 
            yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f; 

        yDeg = ClampAngle (yDeg, yMinLimit, yMaxLimit); 

        // set camera rotation 
        Quaternion rotation = Quaternion.Euler (yDeg, xDeg, 0); 

        // calculate desired camera position
        vTargetOffset = new Vector3 (0, -targetHeight, 0);
        Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset); 

        RaycastHit collisionHit; 
        Vector3 trueTargetPosition = new Vector3 (target.position.x, target.position.y + targetHeight, target.position.z); 

        
        bool isCorrected = false; 

        if (Physics.Linecast (trueTargetPosition, position, out collisionHit, collisionLayers.value)) 
        { 
            correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall; 
            isCorrected = true;
        }

        currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance; 

        currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance); 

        position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset); 
        
        transform.rotation = rotation; 
        transform.position = position; 

    } 

    private static float ClampAngle (float angle, float min, float max) 
    { 
        if (angle < -360) 
            angle += 360; 
        if (angle > 360) 
            angle -= 360; 
        return Mathf.Clamp (angle, min, max); 
    } 
}

I had a similar problem with the thing I was trying to work with. What I ended up doing was: - parent the camera to the player - make the left-right axis turn only the player left-right - - Because the camera is parented to the player, the camera will always follow him perfectly - make the up-down axis tilt only the camera It seems to work for me. As far as I can tell, it's the exact same system they used with the 3rd person mode in Fallout 3/Vegas. Just keep in mind that you generally want to move the player in relation to the Camera's transform, not to the player model's transform

1 Answer

1
  • make the left-right axis turn only the player left-right.
  • make the up-down axis tilt only the camera

That solved it, thanks @SilverTabby