0
votes

updated CODE .. the problem is that i cant move the ball only if i hold the E button than i use WASD or arrows to move the ball...and when i hold Q the camera 2 activates and i can move the ball with WASD or arrows...but with the right axis for that angle...is like ballance that game... i want to not hold the E or Q just to change the camera and move the ball corecctly for that angle with the right axis for the ball but not holding the cameras button

using UnityEngine; using System.Collections;

public class controlPlayer : MonoBehaviour {

public float viteza ; // this is speed
  ;


 void Start(){

}
void Update()
{  


    if(Input.GetKey(KeyCode.Q) )  // if i hold Q the camera 2 activates and with WASD i can move the ball

    {  
        float miscaOrizontal = Input.GetAxis("Horizontal");
        float miscaVertical = Input.GetAxis("Vertical");
        Vector3 miscare = new Vector3(miscaVertical,0.0f,(-miscaOrizontal)); //also here i chande the axis for the camera 2
        rigidbody.AddForce(miscare * viteza * Time.deltaTime);

    }    
    if(Input.GetKey(KeyCode.E) ) // here when i press E the camera 1 activates and alos i can move the ball if i keep pressing E

    {   float miscaOrizontal2 = Input.GetAxis("Horizontal");
        float miscaVertical2 = Input.GetAxis("Vertical");
        Vector3 miscare2 = new Vector3(miscaOrizontal2,0.0f,miscaVertical2); // here are the controls for the ball for the camera ..i had to change the axis here..
        rigidbody.AddForce(miscare2 * viteza * Time.deltaTime);

    }


}






}

}

1

1 Answers

0
votes

Alright, so if I'm understanding you correctly, you want the ball to move in relation to which direction the camera is currently facing. As well as you don't want to have to hold down "Q" or "E" while you move your ball with WASD controls.

So there are a few things that I'd like to mention here, so we'll take them one at a time. First off, you shouldn't have "AddForce" inside of an Update call. Update is called every frame whenever it can. It's great for input, but generally you want to call it in "FixedUpdate" instead. It provides a better response across many devices. If you leave it in update, you can get inaccurate physics results, and missed collisions. There's a lot out there on this subject, so if you want to know more about it, then just google for a little while.

As for the code side, you want to store a reference to what camera you're using to avoid having to hold these buttons down.

What I mean is:

private Camera referencedCamera = null;

private void Start()
{
    referencedCamera = camera1;
}

private void Update()
{
    if(Input.GetKey(KeyCode.Q)) referencedCamera = camera2;
    else if(Input.GetKey(KeyCode.E)) refrencedCamera = camera1;
}

This way you can reference which camera is being used, rather than a key input. Something else to look into is the "State Design Pattern". Below is a video over the "State Design Pattern." That is a great video tutorial series that Derek Banas put up on design patterns, extremely recommend watching them, but the state pattern could potentially solve this problem for you as well. So that should take care of not having to hold the button down in order to make the move. More or less, the buttons will now allow you to just switch which camera is currently being used.

https://www.youtube.com/watch?v=MGEx35FjBuo

So now that that is solved, you want to transform the input that you currently have, and you want to put it in terms of hte camera that is currently being used. So to avoid rewriting a lot of the code over and over again, just know that these two code snippets should be pieced together into one. So "referencedCamera" should be already defined in the code I'm about to write:

private void FixedUpdate()
{
    if(referencedCamera != null)
    {
        float miscaOrizontal = Input.GetAxis("Horizontal");
        float miscaVertical = Input.GetAxis("Vertical");
        Vector3 miscare = referencedCamera.transform.TransformDirection(
            new Vector3(miscaVertical, 0.0f, -miscaOrizontal));
        rigidbody.AddForce(miscare * viteza * Time.deltaTime);
    }
}

So now with this, is it's the same code, but there is a call to the referenced cameras transform to take the vector 3 from the input and put it in relation to the camera instead, and add the new relational vector to the object.

I also want to recommend you looking into events rather than if checks in the update function, but that is beyond the scope of the question. Hope this helps and any questions, don't hesitate to ask.

EDIT After further discussion, we came across something that should also be mentioned. If your camera that you are referencing is angled, the force will be applied in that angle, so keep that in mind. If it is pointed towards an object downward, then forward relative to the camera will apply a force downward, and not entirely in what you may consider the forward direction. Thought it was worth mentioning.