Search Unity

Help With Camera axis

Discussion in 'Scripting' started by Daniel2077xd, May 12, 2022.

  1. Daniel2077xd

    Daniel2077xd

    Joined:
    Jan 27, 2021
    Posts:
    6
    hello I have this script and I would like to know how to implement the rotation of the player so that it is always the one of the camera


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerController : MonoBehaviour
    {
    public FixedJoystick moveJoystick;
    public float fuerzaDeSalto = 8f;
    public bool puedoSaltar;
    public Animator anim;
    public AudioSource jumpxd;
    public ParticleSystem polvo;
    private Rigidbody rb;
    private float moveH,moveV,speedMove=22;
    void Start()

    {
    rb = GetComponent<Rigidbody>();
    polvo.Stop();
    }



    void movePlayer()
    {
    moveH = moveJoystick.Horizontal;
    moveV = moveJoystick.Vertical;
    Vector3 dir = new Vector3(moveH,0, moveV);
    rb.velocity = new Vector3(moveH*speedMove, rb.velocity.y, moveV*speedMove);

    if(dir != Vector3.zero)
    {

    transform.LookAt(transform.position + dir);
    polvo.Play();
    }

    if(dir != Vector3.zero)
    {



    }

    if(dir != Vector3.zero)
    {
    anim.SetBool("Caminar", true);

    }
    else
    anim.SetBool("Caminar", false);

    }








    void Update()
    {
    movePlayer();

    if (puedoSaltar)
    {

    if (Input.GetKeyDown("space"))
    {
    anim.Play("Saltar");

    rb.AddForce(new Vector3(0,fuerzaDeSalto,0), ForceMode.Impulse);
    jumpxd.Play();

    }

    }
    else
    {
    EstoyCayendo();

    }



    }


    public void EstoyCayendo()

    {

    anim.SetBool("Saltar", false);

    }


    public void xd()
    {


    if (puedoSaltar){
    anim.Play("Saltar");
    rb.AddForce(new Vector3(0,fuerzaDeSalto,0), ForceMode.Impulse);
    jumpxd.Play();
    polvo.Play();

    }
    else{

    }
    }


    }
     
  2. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Firstly always use code tags when you post code snippets, Secondly if I get you right, just make the camera a child of a player
     
  3. Daniel2077xd

    Daniel2077xd

    Joined:
    Jan 27, 2021
    Posts:
    6
    does not work, the player when turning back the player does not go to the established direction
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Nice wall of completely unreadable text!

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.

    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.