【发布时间】:2015-12-07 11:11:20
【问题描述】:
我有这段代码,我已经在旧代码中移动了我的游戏对象,但在这段代码中我的玩家不想向上或向下移动。即使我在刚体设置中选择“UseGravity”,游戏对象也不会向下移动!有什么问题?
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.025f, 0);
GetComponent<Rigidbody>().AddForce(movement * speed * 3);
GetComponent<Rigidbody>().velocity = movement * speed / 2;
GetComponent<Rigidbody>().position = new Vector3
(
Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 2f, GetComponent<Rigidbody>().velocity.x * -tilt);
}
}
【问题讨论】:
标签: c# unity3d scripting game-physics gameobject