Search Unity

ThirdpersonInput cause error in 2019 3.14v

Discussion in 'Scripting' started by Arbor77, May 21, 2020.

  1. Arbor77

    Arbor77

    Joined:
    Nov 24, 2019
    Posts:
    1


    below two scripts comes out an error. what should i write in the second script '???' ?




    using System;
    using UnityEngine;
    using UnityStandardAssets.CrossPlatformInput;

    namespace UnityStandardAssets.Characters.ThirdPerson
    {
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class ThirdPersonUserControl : MonoBehaviour
    {

    private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
    private Transform m_Cam; // A reference to the main camera in the scenes transform
    private Vector3 m_CamForward; // The current forward direction of the camera
    private Vector3 m_Move;
    private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input.


    private void Start()
    {
    // get the transform of the main camera
    if (Camera.main != null)
    {
    m_Cam = Camera.main.transform;
    }
    else
    {
    Debug.LogWarning(
    "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
    // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
    }

    // get the third person character ( this should never be null due to require component )
    m_Character = GetComponent<ThirdPersonCharacter>();
    }


    private void Update()
    {
    if (!m_Jump)
    {
    m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    }
    }

    // Fixed update is called in sync with physics
    private void FixedUpdate()
    {
    // read inputs
    float h = CrossPlatformInputManager.GetAxis("Horizontal");
    float v = CrossPlatformInputManager.GetAxis("Vertical");
    bool crouch = Input.GetKey(KeyCode.C);



    // calculate move direction to pass to character
    if (m_Cam != null)
    {
    // calculate camera relative direction to move:
    m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
    m_Move = v*m_CamForward + h*m_Cam.right;
    }
    else
    {
    // we use world-relative directions in the case of no main camera
    m_Move = v*Vector3.forward + h*Vector3.right;
    }
    #if !MOBILE_INPUT
    // walk speed multiplier
    if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f;
    #endif

    // pass all parameters to the character control script
    m_Character.Move(m_Move, crouch, m_Jump);
    m_Jump = false;
    }
    }
    }

    --------------------------------------------------------------------------------
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityStandardAssets.Characters.ThirdPerson;
    using UnityStandardAssets.CrossPlatformInput;

    public class ThirdPersonInput : MonoBehaviour
    {

    public FixedJoystick LeftJoystick;
    public FixedButton Button;
    public FixedTouchField TouchField;
    protected ThirdPersonUserControl Control;

    protected float CameraAngle;
    protected float CameraAngleSpeed = 0.2f;

    // Use this for initialization
    void Start()
    {
    Control = GetComponent<ThirdPersonUserControl>();

    }

    // Update is called once per frame
    void Update()
    {
    Control.??? = LeftJoystick.inputVector.x = ;
    Control.??? = LeftJoystick.inputVector.y = ; // LeftJoystick.inputVector.x

    CameraAngle += TouchField.TouchDist.x * CameraAngleSpeed;

    Camera.main.transform.position = transform.position + Quaternion.AngleAxis(CameraAngle, Vector3.up) * new Vector3(0, 3, 4);
    Camera.main.transform.rotation = Quaternion.LookRotation(transform.position + Vector3.up * 2f - Camera.main.transform.position, Vector3.up);

    }
    }
     
  2. sStarS

    sStarS

    Joined:
    Mar 27, 2020
    Posts:
    19
    the tutorial works just fine , you simply do not understand what he did there and you are missing a few things
    if you did not look in the tutorial description the guy posted a link to GitHub so just copy paste the FixedButton script to add on your jump button and TouchField to add it onto an image in canvas and to make the image invisible simply change the Color value Alpha from 255 to 0 , this is the link to the scripts https://gist.github.com/ditzel/0257d74a7a04626efce7bd1f7a6cfaa0

    also if you want to better understand the touch input follow this tutorial takes 20 minutes
    https://learn.unity.com/tutorial/touch-input-for-mobile-scripting-2019#

    this is my script ( only works if you did not change names in standard asset scripts )

    using System.Collections;
    using System.Collections.Generic;
    using System.Security.Cryptography;
    using UnityEngine;
    using UnityStandardAssets.Characters.ThirdPerson;

    public class ThirdPersonInput : MonoBehaviour
    {
    public FixedJoystick LeftJoystick;
    public FixedButton Button;
    public FixedTouchField Touchfield;

    protected ThirdPersonUserControl Control;
    protected float CameraAngle;
    protected float CameraAngleSpeed = 0.3f;

    // Start is called before the first frame update
    void Start()
    {
    Control = GetComponent<ThirdPersonUserControl>();
    }

    // Update is called once per frame
    void Update()
    {
    Control.m_Jump = Button.Pressed;

    // As you can see h stands for Horizontal and v for Vertical I left it as it was in standard asset script and I used it here
    //Don't forget to make m_Jump public and also call public float h; and public float v; after it hide all 3 in inspector
    //Also remember to go in the joystick pack script and change private Vector2 into public Vector 2 and it will work
    Control.h = LeftJoystick.Direction.x;
    Control.v = LeftJoystick.Direction.y;
    CameraAngle += Touchfield.TouchDist.x * CameraAngleSpeed;
    Camera.main.transform.position = transform.position + Quaternion.AngleAxis(CameraAngle, Vector3.up)* new Vector3(0, 2, -2);
    Camera.main.transform.rotation = Quaternion.LookRotation(transform.position + Vector3.up * 1f - Camera.main.transform.position, Vector3.up);
    }
    }