滚球比赛,当我使用S(向后)球飞

时间:2016-12-20 19:30:43

标签: c# unity3d game-physics

我使用c#进行统一游戏。在这场比赛中,我有一个滚球。我想用鼠标和WASD键旋转相机。

这意味着当我使用W时它应该前进,但是如果我旋转了我的相机,前进方向也应该改变(所以相机总是在球后面) 我做了一个完成这项工作的脚本,但是有一个问题; 当我用S键返回时,球开始向后飞!我该如何解决这个问题?

我认为这是因为观看球的相机角度。 另外,当我前进时,速度有点慢,所以我认为相机的角度对移动有一定的影响。也许我应该删除角度效果

这是我的剧本:

播放器控制脚本

 using UnityEngine;
 using System.Collections;

 public class playercontroller2 : MonoBehaviour
 {
     public float speed;
     private Rigidbody rb;
     void Start()
     {
         rb = GetComponent<Rigidbody>();

     }
     // Use this for initialization
     void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");

         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        movement = Camera.main.transform.TransformDirection(movement);// this make ball goes in right direction ( W will be the place that  camera looking )
        rb.AddForce(movement * speed * Time.deltaTime);
     }
 }

相机脚本

public float turnSpeed = 4.0f;
public Transform player;

private Vector3 offset;

public float zoomStep = 30f;
public float zoomSpeed = 5f;
private float heightWanted;
private float distanceWanted;

private Vector3 zoomResult;
private Quaternion rotationResult;
private Vector3 targetAdjustedPosition;
public float height = 20f;
public float distance = 20f;
public float min = 10f;
public float max = 60;


void Start () {
   // _offset = new Vector3(0, 22, 33);
  offset = transform.position - player.transform.position;
  offset = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f);

}


// Update is called once per frame
void LateUpdate()
{
    offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
    transform.position = player.position + offset;
    transform.LookAt(player.position);


}
void Update()
{

        float mouseInput = Input.GetAxis("Mouse ScrollWheel");
        heightWanted -= zoomStep * mouseInput;
        distanceWanted -= zoomStep * mouseInput;

        // Make sure they meet our min/max values.
        heightWanted = Mathf.Clamp(heightWanted, min, max);
        distanceWanted = Mathf.Clamp(distanceWanted, min, max);

        height = Mathf.Lerp(height, heightWanted, Time.deltaTime * zoomSpeed);
        distance = Mathf.Lerp(distance, distanceWanted, Time.deltaTime * zoomSpeed);

        // Post our result.
        zoomResult = new Vector3(0f, height, -distance);

   }
}

所以要明确的是,当我使用这个脚本时,当我向后按(S或向下箭头)时,球开始向后飞,但我不会在其他方向上遇到这个问题。

2 个答案:

答案 0 :(得分:0)

为什么要转换Vector3

只需在播放器控件脚本中注释这一行:

movement = Camera.main.transform.TransformDirection(movement);

答案 1 :(得分:0)

问题是您正在应用的力矢量都与相机轴而不是世界轴一致。

您需要做的是仅围绕Y轴更改旋转,但仅保留方向的所有其他部分。这可以使用Quaternion.LookRotation来修复旋转。

我不会在我面前团结一致来测试,但我认为这会做你需要的。

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Transform temp = Camera.main.transform;

//Fix the rotation to be flat with the Up vector.
temp.rotation = Quaternion.LookRotation(Camera.main.transform.forward)

//Apply the transform to the movement
movement = temp.TransformDirection(movement);

rb.AddForce(movement * speed * Time.deltaTime);

编辑:这是一个更新的更简单的版本,可以更好地工作,这只是抓住相机围绕y轴的旋转,然后将你的运动矢量转换相同的数量。

void FixedUpdate()
 {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical) 

    //Fix the rotation to match the direction of the camera's y axis.
    Vector3 fixedMovement = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0) * movement;

    rb.AddForce(fixedMovement * speed * Time.deltaTime);
}