Domanda

So far I have a player who moved forward left right and backwards, however how I have it set up it's forward as in forward on the grid, not forward where my player is looking. I have the camera set as FPS, with it following the player around. Now I want to have the player be able to look with my MouseLook script (works already) but based on that I want forward to be wherever the player is looking, not the primitive version I have. I'll post code below to let you know what I am talking about, ANY help is appreciated.

Player Script (movement)

    void Update () {

    if(Input.GetKey (moveU))
    {
        SetTransformZ((transform.position.z) + playerSpeed);

    }

    if(Input.GetKey (moveD))
    {
        SetTransformZ((transform.position.z) - playerSpeed);

    }

    if(Input.GetKey (moveL))
    {
        SetTransformX((transform.position.x) - playerSpeed);
    }

    if(Input.GetKey (moveR))
    {
        SetTransformX((transform.position.x) + playerSpeed);
    }

    else
    {
        rigidbody.angularVelocity = Vector3.zero;

    }


}

void SetTransformX(float n)
{
    transform.position = new Vector3(n, transform.position.y, transform.position.z);
}

void SetTransformZ(float n)
{
    transform.position = new Vector3(transform.position.x, transform.position.y, n);
}

MouseLook script (attached to Main Camera in Unity, this works witht he player so it goes whereever my mouse is looking, but the movement is off since my code to move is very primitive)

[AddComponentMenu("Camera-Control/Mouse Look")]

public class MouseLook : MonoBehaviour {

public enum RotationAxes { 
    MouseXAndY = 0, 
    MouseX = 1, 
    MouseY = 2 }

public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;

void Update ()      
{
    if (axes == RotationAxes.MouseXAndY)
    {
        float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
        rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
        rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
        transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    }

    else if (axes == RotationAxes.MouseX)
    {
        transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    }

    else
    {
        rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
        rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
        transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
    }
}



void Start ()
{
    //if(!networkView.isMine)
    //enabled = false;

    // Make the rigid body not change rotation
    //if (rigidbody)
    //rigidbody.freezeRotation = true;

}
È stato utile?

Soluzione

Okay, so you want your player to move forward, left, right, back relative to where they are looking, as I understand it.

First, make sure that transform.forward is the right direction for your player. in the editor you can select your player gameObject and see if the blue arrow faces the same direction as your player. If not, check out this thread on the unity answers forum to see how to change it.

Here are some functions to put into your player controller script to move relative to player rotation rather than position. (Note: check out Time.deltaTime)

private void moveForward(float speed) {
    transform.localPosition += transform.forward * speed * Time.deltaTime;
}

private void moveBack(float speed) {
    transform.localPosition -= transform.forward * speed * Time.deltaTime;
}

private void moveRight(float speed) {
    transform.localPosition += transform.right * speed * Time.deltaTime;
}

private void moveLeft(float speed) {
    transform.localPosition -= transform.right * speed * Time.deltaTime;
}

Note: transform.position vs transform.localPosition

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top