C# 玩家遇到物体时会抖动
我一直在试图修复这个播放器冲突脚本,但我不知道为什么。它应该阻止玩家穿过墙壁,但是当我的玩家碰到墙壁时,它会在一吨左右晃动,就像脚本运行速度不够快一样。(此代码来自Sebastian Lagues编码教程系列。) 剧本:C# 玩家遇到物体时会抖动,c#,unity3d,collision-detection,C#,Unity3d,Collision Detection,我一直在试图修复这个播放器冲突脚本,但我不知道为什么。它应该阻止玩家穿过墙壁,但是当我的玩家碰到墙壁时,它会在一吨左右晃动,就像脚本运行速度不够快一样。(此代码来自Sebastian Lagues编码教程系列。) 剧本: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerCollision : MonoBehaviou
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
Rigidbody myRigidbody;
public float speed = 5f;
Vector3 velocity;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
Vector3 direction = input.normalized;
velocity = direction * speed;
}
void FixedUpdate() {
myRigidbody.position += velocity * Time.deltaTime;
}
void OnTriggerEnter(Collider triggerCollider) {
print(triggerCollider.gameObject.name);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家集合:单一行为
{
刚体;
公共浮子速度=5f;
矢量3速度;
//在第一帧更新之前调用Start
void Start()
{
myRigidbody=GetComponent();
}
//每帧调用一次更新
无效更新()
{
Vector3 input=newvector3(input.GetAxisRaw(“水平”),0,input.GetAxisRaw(“垂直”);
矢量3方向=输入。归一化;
速度=方向*速度;
}
void FixedUpdate(){
myRigidbody.position+=速度*时间增量;
}
void ontriggenter(碰撞触发碰撞器){
打印(triggerCollider.gameObject.name);
}
}
当我在播放器上启用插值并使用MovePosition时,它抖动得更厉害。尝试将刚体的
碰撞检测模式设置为连续
我还将删除您的velocity
变量,并在FixedUpdate
中设置myRigidbody.velocity
,如下所示:
void Update() {
}
void FixedUpdate() {
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
Vector3 direction = input.normalized;
myRigidbody.velocity = direction * speed;
}
尝试将刚体的碰撞检测模式设置为连续
我还将删除您的velocity
变量,并在FixedUpdate
中设置myRigidbody.velocity
,如下所示:
void Update() {
}
void FixedUpdate() {
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
Vector3 direction = input.normalized;
myRigidbody.velocity = direction * speed;
}
非常感谢您!唯一的问题是我必须冻结旋转,但我不需要旋转。非常感谢!唯一的问题是我必须冻结旋转,但我不需要旋转。