Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Moving a object in Camera Direction

Discussion in 'Scripting' started by brawler93, Aug 1, 2018.

  1. brawler93

    brawler93

    Joined:
    Mar 22, 2016
    Posts:
    2
    hey guys,

    I'm trying to make some kinda of roll a ball game in 3rd person. The ball moves forward, backwards, to the left or right depending on the input you give. The movement is based on where the camera is facing(I can move my mouse to change the camera's position). Im moving my ball with addforce, but I'm unable to get the ball to move in the right way. The only way Ive been able to manage to get the movement somewhat right is by setting angles and rotating the vectors, but I want it by vectors

    this is the code by angles:
    Code (CSharp):
    1.         float angle = 0;
    2.         isMoving = false;
    3.  
    4.         if (Input.GetKey(InputManager.IM.forward))
    5.         {
    6.             angle += 0;
    7.             isMoving = true;
    8.         }
    9.  
    10.         // transform.position += Vector3.forward / 2;
    11.  
    12.         if (Input.GetKey(InputManager.IM.backward))
    13.         {
    14.             angle += 180;
    15.             isMoving = true;
    16.         }
    17.         // transform.position += -Vector3.forward / 2;
    18.  
    19.         if (Input.GetKey(InputManager.IM.left))
    20.         {
    21.             angle -= 90;
    22.             isMoving = true;
    23.         }
    24.  
    25.         //transform.position += Vector3.left / 2;
    26.  
    27.         if (Input.GetKey(InputManager.IM.right))
    28.         {
    29.             angle += 90;
    30.             isMoving = true;
    31.         }
    32.  
    33.         angle *= Mathf.Deg2Rad;
    34.         // transform.position += Vector3.right / 2;
    35.  
    36.  
    37.  
    38.         hDir = MainCamera.transform.forward;
    39.         hDir.y = 0;
    40.  
    41.         Force = new Vector3(hDir.x * Mathf.Cos(angle) + hDir.z * Mathf.Sin(angle), 0, -hDir.x * Mathf.Sin(angle) + hDir.z * Mathf.Cos(angle));
    42.      
    43.  
    44.         if (isMoving == true)
    45.         {
    46.             rb.AddForce(Force * 1000);
    47.         }
    48.         rb.velocity *= 0.5f;

    and this is what I want:
    Code (CSharp):
    1. float x = 0;
    2.         float z = 0;
    3.  
    4.         if (Input.GetKey(InputManager.IM.forward))
    5.         {
    6.             z += 1;
    7.         }
    8.  
    9.         // transform.position += Vector3.forward / 2;
    10.  
    11.         if (Input.GetKey(InputManager.IM.backward))
    12.         {
    13.             z -= 1;
    14.         }
    15.         // transform.position += -Vector3.forward / 2;
    16.  
    17.         if (Input.GetKey(InputManager.IM.left))
    18.         {
    19.             x -= 1;
    20.         }
    21.  
    22.         //transform.position += Vector3.left / 2;
    23.  
    24.         if (Input.GetKey(InputManager.IM.right))
    25.         {
    26.             x += 1;
    27.         }
    28.  
    29.         hDir = MainCamera.transform.forward;
    30.         hDir.y = 0;
    31.  
    32.         Force = ??????
    33.      
    34.  
    35.         if (x != 0 || z != 0)
    36.         {
    37.             rb.AddForce(Force * 1000);
    38.         }
    39.         rb.velocity *= 0.5f;
    40.         */
    Any help is highly appriciated
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Haven't tested this code, but maybe something like this? :-
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         Vector3 vForce = Vector3.zero;
    4.  
    5.         if (Input.GetKey(InputManager.IM.forward))
    6.         {
    7.             vForce = MainCamera.transform.forward;
    8.         }
    9.  
    10.         if (Input.GetKey(InputManager.IM.backward))
    11.         {
    12.             vForce = -MainCamera.transform.forward;
    13.         }
    14.  
    15.         if (Input.GetKey(InputManager.IM.left))
    16.         {
    17.             vForce = -MainCamera.transform.right;
    18.         }
    19.  
    20.         if (Input.GetKey(InputManager.IM.right))
    21.         {
    22.             vForce = MainCamera.transform.right;
    23.         }
    24.  
    25.         vForce.y = 0;
    26.  
    27.         if (vForce.x != 0 || vForce.z != 0)
    28.         {
    29.             vForce.Normalize();
    30.             rb.AddForce(vForce * 1000);
    31.         }
    32.     }