Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Move this character based on camera look direction (Solved)

Discussion in 'Made With Unity' started by tijanikun, Nov 10, 2018 at 9:30 PM.

  1. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    14
    Hello,

    I realy need help to make my character dash/fly toward based on camera view when i press V, couldn't make that happen with the tutorials i found on google.

    You'll be my god if you help me for that, please.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.Events;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.  
    9.     public float moveSpeed;
    10.     //public Rigidbody theRD;
    11.     public float jumpForce;
    12.     public CharacterController controller;
    13.     public float gravityScale;
    14.     public Animator anim;
    15.     public Transform pivot;
    16.     public float rotateSpeed;
    17.     public float runSpeed;
    18.     public float attackSpeed = 30;
    19.     public float omegaSpeed = 50;
    20.     public float clawSpeed = 30;
    21.  
    22.     public GameObject Camera1;
    23.     public GameObject OmeCamera;
    24.  
    25.     public Camera Camera2;
    26.     public Camera OmeCamera2;
    27.  
    28.     public int CamMode;
    29.    
    30.  
    31.  
    32.     public float attack1;
    33.     float timestamp;
    34.  
    35.     float lastAttack;
    36.     float interval = 5;
    37.  
    38.  
    39.     public AudioSource SwordSound;
    40.     private IEnumerator coroutine;
    41.     private IEnumerator coroutine2;
    42.    
    43.  
    44.     float t = 0;
    45.  
    46.     float t1;
    47.  
    48.     Rigidbody  Rigid;
    49.  
    50.  
    51.     public float cooldowntime = 1;
    52.  
    53.     public float cooldowntime2 = 4;
    54.     public float cooldowntime3 = 4;
    55.  
    56.  
    57.  
    58.     public float nextFireTime = 0;
    59.  
    60.     public float nextFireTime2 = 0;
    61.  
    62.  
    63.     public float nextFireTime3 = 0;
    64.  
    65.     bool charge;
    66.  
    67.     public AudioSource ChargeUp;
    68.  
    69.     public Transform target;
    70.     private bool Froze;
    71.  
    72.     public int myNumber = 0;
    73.  
    74.     public float knockBackForce;
    75.     public float knockBackTime;
    76.     private float knockBackCounter;
    77.  
    78.  
    79.     private Vector3 moveDirection;
    80.     public GameObject playerModel;
    81.  
    82.     private Transform cameraTransform;
    83.     public Transform cameraTransform2;
    84.    
    85.  
    86.  
    87.     // Use this for initialization
    88.     void Start()
    89.     {
    90.         //theRD = GetComponent<Rigidbody>();
    91.         controller = GetComponent<CharacterController>();
    92.         coroutine2 = (MoveOverSeconds(gameObject, new Vector3(0f, 0f, 0f), 2f));
    93.         cameraTransform = Camera.main.transform;
    94.  
    95.         coroutine = (GaiaOverSeconds(gameObject, new Vector3(0f, 0f, 0f), 0.5f));
    96.  
    97.  
    98.     }
    99.  
    100.  
    101.     // Update is called once per frame
    102.     void Update()
    103.     {
    104.  
    105.         // theRD.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, theRD.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
    106.  
    107.         /*if (Input.GetButtonDown("Jump"))
    108.            {
    109.                         theRD.velocity = new Vector3(theRD.velocity.x, jumpForce, theRD.velocity.z); */
    110.         //moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
    111.         if (Input.GetKeyDown(KeyCode.B))
    112.         {
    113.             moveSpeed = runSpeed;
    114.         }
    115.         if (Input.GetKeyUp(KeyCode.B))
    116.         {
    117.             moveSpeed = 10;
    118.         }
    119.         if (Input.GetButton("Mouse1"))
    120.         {
    121.             anim.Play("WarFootHit");
    122.  
    123.             GameObject foot = GameObject.FindWithTag("WarFoot");
    124.             (foot.GetComponent(typeof(BoxCollider)) as Collider).enabled = true;
    125.             StartCoroutine(Attack1(1f));
    126.  
    127.         }
    128.  
    129.  
    130.         //nextFireTime = Time.time + cooldowntime;
    131.         //}
    132.  
    133.         if (Time.time > nextFireTime)
    134.         {
    135.             if (Input.GetKeyDown(KeyCode.A))
    136.             {
    137.  
    138.                 StartCoroutine(OmeCameraEnu());
    139.                 anim.Play("Omegamon");
    140.  
    141.  
    142.                 StartCoroutine(MoveOverTornado(gameObject, new Vector3(0f, 0f, 0f), 0.5f));
    143.                 StartCoroutine(Sword(1.2f));
    144.  
    145.                 nextFireTime = Time.time + cooldowntime;
    146.             }
    147.         }
    148.         if (Time.time > nextFireTime3)
    149.         {
    150.             if (Input.GetKeyDown(KeyCode.V))
    151.             {              
    152.                 anim.Play("BraveTornado");
    153.                 StartCoroutine(MoveOverTornado(gameObject, new Vector3(0f, 0f, 0f), 2f));
    154.                
    155.                 nextFireTime3 = Time.time + cooldowntime3;
    156.             }
    157.         }
    158.  
    159.  
    160.         if (Time.time > nextFireTime)
    161.         {
    162.             if (Input.GetKeyDown(KeyCode.F))
    163.             {
    164.                 anim.Play("WarGreymonCharge");
    165.  
    166.                 nextFireTime = Time.time + cooldowntime;
    167.             }
    168.         }
    169.  
    170.         if (Time.time > nextFireTime2)
    171.         {
    172.  
    173.             //if (Input.GetButton("PressV"))
    174.             //{
    175.             //  if (coroutine2 != null)
    176.             //   StopCoroutine(coroutine2);
    177.             // StartCoroutine(MoveOverSeconds(gameObject, new Vector3(0f, 0f, 0f), 1f));
    178.  
    179.  
    180.             //nextFireTime = Time.time + cooldowntime;
    181.             //}
    182.            
    183.             if(Input.GetKeyDown(KeyCode.E))
    184.             {
    185.  
    186.                 StartCoroutine(MoveOverSeconds(gameObject, new Vector3(0f, 0f, 0f), 2f));
    187.  
    188.  
    189.                 nextFireTime2 = Time.time + cooldowntime2;
    190.             }
    191.         }
    192.        
    193.         if (Froze == true)
    194.         {
    195.             moveSpeed = 0;
    196.  
    197.         }
    198.  
    199.  
    200.         if (knockBackCounter <= 0)
    201.         {
    202.             float yStore = moveDirection.y;
    203.             moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
    204.             moveDirection = moveDirection.normalized * moveSpeed;
    205.             moveDirection.y = yStore;
    206.  
    207.             if (controller.isGrounded)
    208.             {
    209.                 moveDirection.y = 0f;
    210.                 if(lastAttack + interval < Time.time)
    211.                 if (Input.GetButtonDown("Jump"))
    212.                 {
    213.  
    214.                     moveDirection.y = jumpForce;
    215.                 }
    216.             }
    217.         }
    218.         else
    219.         {
    220.             knockBackCounter -= Time.deltaTime;
    221.         }
    222.  
    223.         moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    224.         controller.Move(moveDirection * Time.deltaTime);
    225.  
    226.  
    227.         //Move the player in different direction based on camera look direction
    228.         if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
    229.         {
    230.             transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
    231.             Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
    232.             playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
    233.         }
    234.  
    235.         anim.SetBool("isGrounded", controller.isGrounded);
    236.         anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal"))));
    237.  
    238.         if (Input.GetKey(KeyCode.F))
    239.             charge = true;
    240.  
    241.         //Otherwise the GameObject cannot jump.
    242.         else charge = false;
    243.  
    244.         //If the GameObject is not jumping, send that the Boolean “Jump” is false to the Animator. The jump animation does not play.
    245.         if (charge == false)
    246.             anim.SetBool("Charge", false);
    247.  
    248.         //The GameObject is jumping, so send the Boolean as enabled to the Animator. The jump animation plays.
    249.         if (charge == true)
    250.             anim.SetBool("Charge", true);
    251.  
    252.  
    253.     }
    254.  
    255.     public IEnumerator Sword(float waitTime)
    256.     {
    257.         yield return new WaitForSeconds(waitTime);
    258.         SwordSound.Play();
    259.     }
    260.  
    261.     public IEnumerator MoveOverSeconds(GameObject objectToMove, Vector3 end, float seconds)
    262.     {
    263.         float elapsedTime = 0;
    264.         Vector3 startingPos = objectToMove.transform.position;
    265.  
    266.         while (elapsedTime < seconds)
    267.         {
    268.  
    269.             GameObject war = GameObject.FindWithTag("War");
    270.             (war.GetComponent(typeof(CharacterController)) as Collider).enabled = false;
    271.             objectToMove.transform.position += cameraTransform2.up * Time.deltaTime * attackSpeed;
    272.             gravityScale = 0;
    273.             moveSpeed = 0;
    274.             elapsedTime += Time.deltaTime;
    275.             yield return new WaitForEndOfFrame();
    276.  
    277.             playerModel.transform.rotation = cameraTransform2.rotation;
    278.  
    279.             StartCoroutine(GaiaFly(2.6f));
    280.  
    281.  
    282.         }
    283.     }
    284.  
    285.     public IEnumerator MoveOverTornado(GameObject objectToMove, Vector3 end, float seconds)
    286.     {
    287.         float elapsedTime = 0;
    288.         Vector3 startingPos = objectToMove.transform.position;
    289.  
    290.         while (elapsedTime < seconds)
    291.         {
    292.  
    293.             GameObject claw = GameObject.FindWithTag("Claw");
    294.             (claw.GetComponent(typeof(BoxCollider)) as Collider).enabled = true;
    295.  
    296.             moveDirection = Camera.main.transform.TransformDirection(moveDirection);
    297.             objectToMove.transform.position += cameraTransform.forward * Time.deltaTime * clawSpeed;
    298.             gravityScale = 0;
    299.             elapsedTime += Time.deltaTime;
    300.             yield return new WaitForEndOfFrame();
    301.  
    302.             playerModel.transform.rotation = cameraTransform.rotation;
    303.  
    304.             StartCoroutine(BraveFly(2.6f));
    305.  
    306.  
    307.         }
    308.     }
    309.  
    310.     public IEnumerator MoveOverOmega(GameObject objectToMove, Vector3 end, float seconds)
    311.     {
    312.         float elapsedTime = 0;
    313.         Vector3 startingPos = objectToMove.transform.position;
    314.  
    315.         while (elapsedTime < seconds)
    316.         {
    317.             moveSpeed = 0;
    318.             elapsedTime += Time.deltaTime;
    319.             yield return new WaitForEndOfFrame();
    320.             StartCoroutine(OmegaFront());
    321.             StartCoroutine(OmegaForward(gameObject, 1f, 0.5f));
    322.  
    323.  
    324.  
    325.         }
    326.     }
    327.     public IEnumerator OmegaFront()
    328.     {
    329.         float elapsedTime = 0;
    330.         float seconds = 3;
    331.         while (elapsedTime < seconds)
    332.         {
    333.             elapsedTime += Time.deltaTime;
    334.             yield return new WaitForEndOfFrame();
    335.             playerModel.transform.rotation = cameraTransform2.rotation;
    336.  
    337.         }
    338.     }
    339.  
    340.     public IEnumerator OmegaForward(GameObject objectToMove, float waitTime, float waitTime2)
    341.     {
    342.  
    343.         Vector3 startingPos = objectToMove.transform.position;
    344.         yield return new WaitForSeconds(waitTime);
    345.  
    346.         objectToMove.transform.position += cameraTransform2.forward * Time.deltaTime * omegaSpeed;
    347.         yield return new WaitForSeconds(waitTime);
    348.         moveSpeed = 10;
    349.  
    350.     }
    351.    
    352.  
    353.     public IEnumerator Attack1(float waitTime)
    354.     {
    355.         yield return new WaitForSeconds(waitTime);
    356.         GameObject foot = GameObject.FindWithTag("WarFoot");
    357.         (foot.GetComponent(typeof(BoxCollider)) as Collider).enabled = false;
    358.     }
    359.  
    360.     public IEnumerator GaiaFly(float waitTime)
    361.     {
    362.  
    363.         yield return new WaitForSeconds(waitTime);
    364.         gravityScale = 5;
    365.         moveSpeed = 10;
    366.  
    367.         GameObject war = GameObject.FindWithTag("War");
    368.         (war.GetComponent(typeof(CharacterController)) as Collider).enabled = true;
    369.  
    370.     }
    371.     public IEnumerator BraveFly(float waitTime)
    372.     {
    373.  
    374.         yield return new WaitForSeconds(waitTime);
    375.         gravityScale = 5;
    376.  
    377.         GameObject claw = GameObject.FindWithTag("Claw");
    378.         (claw.GetComponent(typeof(BoxCollider)) as Collider).enabled = false;
    379.  
    380.     }
    381.  
    382.  
    383.     public IEnumerator GaiaOverSeconds(GameObject objectToMove, Vector3 end, float seconds)
    384.     {
    385.         float elapsedTime = 0;
    386.         Vector3 startingPos = objectToMove.transform.position;
    387.         while (elapsedTime < seconds)
    388.         {
    389.             objectToMove.transform.position += cameraTransform.forward * Time.deltaTime * attackSpeed;
    390.             objectToMove.transform.rotation = cameraTransform.rotation;
    391.             elapsedTime += Time.deltaTime;
    392.             yield return new WaitForEndOfFrame();
    393.         }
    394.     }
    395.  
    396.     private IEnumerator OmeCameraEnu()
    397.     {
    398.  
    399.         Camera2.enabled = false;
    400.         OmeCamera2.enabled = true;
    401.  
    402.         yield return new WaitForSeconds(2.80f);
    403.  
    404.  
    405.         Camera2.enabled = true;
    406.         OmeCamera2.enabled = false;
    407.     }
    408.  
    409.     private IEnumerator FrozeIt(float waitTime)
    410.     {
    411.        
    412.         print("true");
    413.         yield return new WaitForSeconds(waitTime);
    414.      
    415.         print("false");
    416.  
    417.  
    418.         GameObject hand = GameObject.FindWithTag("handrightplayer");
    419.         (hand.GetComponent(typeof(Collider)) as Collider).isTrigger = false;
    420.         print("handfalse");
    421.     }
    422.  
    423.     public void Knockback(Vector3 direction)
    424.     {
    425.         knockBackCounter = knockBackTime;
    426.  
    427.         moveDirection = direction * knockBackForce;
    428.         moveDirection.y = knockBackForce;
    429.     }
    430.    
    431.     }
    432.  
     
  2. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    14
    Im sorry sorry sorry for this i found the problem, it was because i didn't set the targettag "Claw" to my weapon...

    Nevermind this thread