【发布时间】:2019-04-05 22:18:31
【问题描述】:
我想做一个弹跳球游戏,我必须控制(连续弹跳的)球的行进方向。问题是,通过增加力,球的速度越来越快,我试图控制它越远,因为刚体上的摩擦力必须为零,我正在使用 Rigidbody().AddForce()。如何在不过度用力的情况下移动它? 如何让弹跳球朝我想要的方向弹跳(移动)? 我不想使用动画,游戏将基于各种物理事件。
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour{
public Color[] colors;
private Renderer renderer;
private void Awake(){
renderer = this.GetComponent<Renderer>();
InvokeRepeating("ChangeColor", 0, 3f);
}
private void FixedUpdate()
{
MoveBall();
}
void ChangeColor(){
renderer.material.color = colors[Random.Range(0, colors.Length)];
}
void MoveBall(){
Debug.Log(GetComponent<Rigidbody>().velocity.magnitude);
if (Input.GetKey("w"))
{
GetComponent<Rigidbody>().AddForce(new Vector3(0, 0, 1) * (30f - (GetComponent<Rigidbody>().velocity.magnitude*9)));
}
if (Input.GetKey("s"))
{
GetComponent<Rigidbody>().AddForce(new Vector3(0, 0, -1) * (30f - (GetComponent<Rigidbody>().velocity.magnitude*9)));
}
if (Input.GetKey("a"))
{
GetComponent<Rigidbody>().AddForce(new Vector3(-1, 0, 0) * (30f - (GetComponent<Rigidbody>().velocity.magnitude*9)));
}
if (Input.GetKey("d"))
{
GetComponent<Rigidbody>().AddForce(new Vector3(1, 0, 0) * (30f - (GetComponent<Rigidbody>().velocity.magnitude*9)));
}
}
}
【问题讨论】: