MultiPlayer 注意点
本篇是对项目的一些记录
public void Move(float horizontal, float vertical){Vector3 movement = new Vector3(horizontal, 0f, vertical);movement = movement.normalized * speed*0.025f;// * Time.deltaTime;PlayerRigidbody.MovePosition(transform.position + movement);}
注意这里的不能乘以,因为要保证每一帧走的距离是一样的而不是随着帧率改变!!
否则再不同客户端会看到每一次Input在不同客户端上的距离不同(不同客户端的帧率不同),造成不同步!
.和.的区别:
.
n;
The of the .
. you to get and set the of a using the . If you the of a ., the will be after the next step. This is than the ., as the will cause all to their to the .
If you want to move a use , which takes into .
比如:
.cs
if (msg.msg_type == (int)RequestType.INPUT){InputMessage Input_msg = msg as InputMessage;viewPlayer.get_Rigidbody().position = new Vector3(viewPlayer.get_Rigidbody().position.x,Input_msg.moving_y, viewPlayer.get_Rigidbody().position.z);viewPlayer.Move(Input_msg.moving_x, Input_msg.moving_z);}
如果使用:
.. = new (...x,., ...z);
如果环境复杂(有很多)就会卡住很久!
例如这种环境:
因为如果使用., 将导致所有附加的重新计算它们相对于刚体的位置。
例如:简单的环境中:
viewPlayer.transform.position = new Vector3(viewPlayer.transform.position.x, Input_msg.moving_y, viewPlayer.transform.position.z);
用完全没有问题
.在下一次时就会改变。
box 不会穿过 ,但如果是 就会穿过去 = =不清楚为啥
最终的设置和设置
代码更改的地方:
. 冻结旋转
: bool
描述
will the of the .
控制物理是否改变物体的旋转。
If is , the is not by the . This is for first , the needs full of the using the mouse.
如果被启用,旋转不会被物体模拟修改。这对于创建第一人称射击游戏时是很有用的,因为玩家需要使用鼠标完全控制旋转。
using UnityEngine;
using System.Collections;public class example : MonoBehaviour {public void Awake() {rigidbody.freezeRotation = true;}
}
Input.
wn();
true the frame the user the given mouse .
You need to call this from , since the state gets reset each frame. It will not true until the user has the mouse and it again. are 0 for the (often the left ), 1 for , and 2 for the .
using UnityEngine;
using System.Collections;// Detects clicks from the mouse and prints a message
// depending on the click detected.public class ExampleClass : MonoBehaviour
{void Update(){if (Input.GetMouseButtonDown(0))Debug.Log("Pressed primary button.");if (Input.GetMouseButtonDown(1))Debug.Log("Pressed secondary button.");if (Input.GetMouseButtonDown(2))Debug.Log("Pressed middle click.");}
}
Drag、 Drag设成了无穷大,所以给推力时动不了,还要记得所有的碰撞要放在里!
把Drag、 Drag改小就好
穿过地面的原因:( 和地面都添加了还时穿过了。。)
不能用.()!!必须通过让运动!,还有所有的运动都要放在()里!!!
让贴着地面走
只需要给,y轴方向一个速度就行!和,一样的道理,相当于给一个重力,并不需要投一条射线再判断交点(卡死 = =)
.((-.) * * speed );
.(.right * * speed);
.((-.up) * 1f);
最终代码:
public class ViewPlayer : MonoBehaviour
{GameObject player_instance;public int connectID;//与Player的connectID相同;float speed = 20f;float rotate_speed = 10f;public GameObject BulletPrefabs;public Transform bulletSpawn;Rigidbody PlayerRigidbody;List msg_list;int touchTerrianMask;float max_y = 100f;float z = 0;float y = 0;float last_y = 2f;float x = 0;float rot_x = 0;float rot_y = 0;int key_x1 = 0;int key_x2 = 0;Transform Transform;public CameraFollow camera;public void Start(){msg_list = new List();PlayerRigidbody = GetComponent();touchTerrianMask = LayerMask.GetMask("touchTerrian");Transform = gameObject.GetComponent();PlayerRigidbody.freezeRotation = true;}public void FixedUpdate(){x += Input.GetAxis("Horizontal");z += Input.GetAxis("Vertical");if (Input.GetKeyDown(KeyCode.Q)){key_x1++;rot_x = 0;}if (Input.GetKeyDown(KeyCode.E)){key_x2++;rot_x = 0;}if (Input.GetKeyUp(KeyCode.Q)){rot_x += key_x1 * (-rotate_speed);key_x1 = 0;}if (Input.GetKeyUp(KeyCode.E)){rot_x += key_x2 * (rotate_speed);key_x2 = 0;}}public List get_local_input(){msg_list.Clear();if (x != 0 || z != 0){CustomSyncMsg input_msg = new InputMessage(connectID, new Vector3(x, 0, z));msg_list.Add(input_msg);}x = 0;z = 0;if (rot_x != 0f || rot_y != 0f){CustomSyncMsg rot_msg = new RotateMessage(connectID, new Vector2(rot_x, rot_y));msg_list.Add(rot_msg);rot_x = 0;rot_y = 0;}return msg_list;}public void Move(float horizontal, float vertical)//, float y{// transform.up =new Vector3(0,0,y);//Transform.position = new Vector3(Transform.position.x, y, Transform.position.z);// Vector3 cur_pos= Transform.position +(-Transform.forward )* horizontal * speed * 0.025f + Transform.right * vertical * speed * 0.025f +(- Transform.up) * 100f;// PlayerRigidbody.MovePosition(cur_pos);PlayerRigidbody.AddForce((-Transform.forward) * horizontal * speed );PlayerRigidbody.AddForce(Transform.right * vertical * speed);PlayerRigidbody.AddForce((-Transform.up) * 1f);//Transform.position = new Vector3(Transform.position.x, y, Transform.position.z);// Debug.Log("PlayerRigidbody.position + movement = " + (PlayerRigidbody.position + movement).x + "PlayerRigidbody.position + movement " + (PlayerRigidbody.position + movement).y);//PlayerRigidbody.AddForce(transform.right * horizontal * speed, ForceMode.Impulse);//PlayerRigidbody.AddForce(transform.forward * vertical * speed, ForceMode.Impulse);}public void Rotate(float delta_x, float delta_y){//现在只有绕自己的y轴旋转Transform.Rotate(Vector3.up, delta_x, Space.Self);Tool.Print("......................Rotating .........delta_x = " + delta_x.ToString());}public float move_on_the_ground(){Ray ray = new Ray(transform.position, -transform.up);RaycastHit hit;if (Physics.Raycast(ray, out hit, max_y, touchTerrianMask)){return hit.point.y + 1f;}elsereturn -101;}public void set_player_instance(GameObject instance){player_instance = instance;}public float get_speed(){return speed;}public Rigidbody get_Rigidbody(){return PlayerRigidbody;}public void bind_cameraFollow(CameraFollow cameraFollow){camera = cameraFollow;}public void bind_playerInstance(GameObject Instance){player_instance = Instance;}
}
Mathf.
(, , ref , , = Mathf., = Time.);
The .
The we are to reach.
The , this value is by the every time you call it.
the time it will take to reach the . A value will reach the .
you to clamp the speed.
The time since the last call to this . By Time..
a value a goal over time.
The value is by some - like , which will never . The can be used to any kind of value, , , .
using UnityEngine;public class Example : MonoBehaviour
{// Smooth towards the height of the targetTransform target;float smoothTime = 0.3f;float yVelocity = 0.0f;void Update(){float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);transform.position = new Vector3(transform.position.x, newPosition, transform.position.z);}
}
.
Other
Leave
TO
tion;
A that the of the in world space.
. a . You can a or the . Do not to edit/.. less than 180 .
. no lock.
To , ., which uses Euler .
using UnityEngine;// Transform.rotation example.// Rotate a GameObject using a Quaternion.
// Tilt the cube using the arrow keys. When the arrow keys are released
// the cube will be rotated back to the center using Slerp.public class ExampleScript : MonoBehaviour
{float smooth = 5.0f;float tiltAngle = 60.0f;void Update(){// Smoothly tilts a transform towards a target rotation.float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;// Rotate the cube by converting the angles into a quaternion.Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);// Dampen towards the target rotationtransform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);}
}
.范围为 -180到180度!!
鼠标的位置,就是最终的位置!而不是旋转差!这样的好处是鼠标定在哪,的旋转角度就固定在哪,否则,若是旋转差的话,会出现鼠标不能与枪口一致的情况
项目中的旋转代码:
//Rotatereadonly float rotate_speed = 4f;float rot_x = 0;float rot_y = 0;int key_x1 = 0;int key_x2 = 0;float currentRotation = 0;float currentVelocity =0.0f;//旋转到targetRaotation的速度public float smoothTime=0.1f;//旋转到targetRaotation所需要的时间float bottomAngle = -45f;float topAngle = 60f;readonly float camRayLength = 100f;Vector3 playerToMouse;public void Rotate(float delta_x, float delta_y){// Transform.Rotate(Vector3.up, delta_x, Space.Self);currentRotation = Mathf.SmoothDamp(currentRotation, delta_x, ref currentVelocity, smoothTime);transform.rotation = Quaternion.Euler(0, currentRotation, 0);//Quaternion targetRotation = new Quaternion(0, Mathf.Sin(delta_x / 2.0f), 0, Mathf.Cos(delta_x / 2.0f));//将角度转化为四元素//Quaternion newRotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.timeScale * 20.0f);//PlayerRigidbody.MoveRotation(newRotation);}
public void FixedUpdate(){PlayerRigidbody.AddForce(Physics.gravity * Mygravity, ForceMode.Acceleration);x += Input.GetAxis("Horizontal");z += Input.GetAxis("Vertical");//RotateInputrot_x += Input.GetAxis("Mouse X") * rotate_speed;rot_y += Input.GetAxis("Mouse Y") * rotate_speed;rot_y = Mathf.Clamp(rot_y, bottomAngle, topAngle);if (Input.GetMouseButton(0)){has_shoot = true;shootRay = Camera.main.ScreenPointToRay(Input.mousePosition);Debug.DrawRay(shootRay.origin, shootRay.direction * 100, Color.yellow);}else{has_shoot = false; }if (isDead){Death();}}