当前位置:网站首页>UNET notes
UNET notes
2022-07-27 00:23:00 【fanfan_ hongyun】
https://blog.csdn.net/zhangxiao13627093203/article/details/81026124
1. Want multiple clients to operate the same object , To make this object generated from the server , add to NetworkIentity and NetworkTransform Make it into prefabricated parts and put it in NetworkManager Register in
2. Empty objects add NetworkIdentity, Its bottom SeverOnly Tick meaning : As a server application , Activate under the server , Deactivation on the client ,LocalPlayerAuthority Tick meaning : As a client application
3. Non server players have no permission to call 【Commend】 Method , But in player Can be called , The objects generated by the server cannot be used directly by the client , It must be called through the server, that is 【Commend】 Method
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Player : NetworkBehaviour
{
private Rigidbody rb;
public float MoveSpeed = 8; // Horizontal and vertical speeds
private Camera Camera;
private void Awake()
{
rb = transform.GetComponent<Rigidbody>();
Camera = Camera.main;
}
private void Update()
{
if (!isLocalPlayer) return;
if (Input.GetKeyDown(KeyCode.Space))
{
CmdOpen_door();
}
}
private void FixedUpdate()
{
if (!isLocalPlayer) return; // If it's not a local role , Just skip.
Vector2 moveDir;
// Get the horizontal and vertical values of keyboard input , Namely 1 and -1, Represents two directions , And to the moveDir This directional variable
if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0)
{
moveDir.x = 0;
moveDir.y = 0;
}
else
{
moveDir.x = Input.GetAxisRaw("Horizontal");
moveDir.y = Input.GetAxisRaw("Vertical");
Move(moveDir);
}
}
private void LateUpdate()
{
if (!isLocalPlayer) return; // If it's not a local role , Just skip.
Camera.transform.position = Vector3.Lerp(Camera.transform.position, this.transform.position + new Vector3(0, 2, -5), 5*Time.deltaTime);
}
void Move(Vector2 direction = default(Vector2))
{
if (direction != Vector2.zero) // If the direction variable is not a zero vector
{
transform.rotation = Quaternion.LookRotation(new Vector3(direction.x,0, direction.y)); // Adjust the direction
//transform.forward It's a variable , It is the direction variable calculated according to the current direction
Vector3 movementDir = transform.forward * MoveSpeed * Time.deltaTime; // The unit vector of the current direction * Speed * Time = The offset of the current direction
rb.MovePosition(rb.position + movementDir);
}
}
[Command]
public void CmdOpen_door()
{
if(Door_span.instance!=null)
Door_span.instance.door.transform.Rotate(Vector3.up * 90);
}
}using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// The script hangs on an empty object And add NetworkIdentity If you check the SeverOnly It can be used Start Call directly
/// If not checked or checked LocalPlayerAuthority Want to generate only in OnStartServer Call in
/// If not checked or checked LocalPlayerAuthority If you want to generate a unique, you can also Span_door The method is changed to 【Commend】 Method , But I don't recommend , This will make the client operate the same but have no permission to issue a warning
/// </summary>
public class Door_span : NetworkBehaviour
{
public static Door_span instance;
[Header(" Door prefabrication ")]
public GameObject door_prefab;
[HideInInspector]
public GameObject door;
private void Awake()
{
instance = this;
}
private void Start()
{
//Span_door();
}
/// <summary>
/// OnStartServer Only once
/// </summary>
public override void OnStartServer()
{
door = Instantiate(door_prefab, Vector3.zero, Quaternion.identity);// receive
NetworkServer.Spawn(door);
}
//public void Span_door()
//{
// print(" Came in ");
//door = Instantiate(door_prefab, Vector3.zero, Quaternion.identity);// receive
//NetworkServer.Spawn(door);
//}
}
边栏推荐
- Recent answers - column
- 滑动窗口问题总结
- Codeforces D. two divisors (number theory, linear sieve)
- 深度学习调参技巧
- 3 esp8266 nodemcu network server
- Recbole use 1
- About no module named'django.db.backends.mysql'
- Anaconda = > pycharm=> CUDA=> cudnn=> pytorch environment configuration
- 今日份20220719折腾deeplabcut
- CCPD data set processing (target detection and text recognition)
猜你喜欢
随机推荐
yolov5在jetson nano上的部署 deepstream
MySQL optimization
C and pointers Chapter 18 runtime environment 18.4 summary
Chapter 1 develop the first restful application
RESNET paper interpretation and code implementation (pytorch)
15_ Key function and principle
4-4 object lifecycle
Relationship between limit, continuity, partial derivative and total differential of multivariate function (learning notes)
PTA 7-1 play with binary tree
画冲击函数
第2章 开发用户流量拦截器
第7章 课程总结
CSDN article syntax rules
10_ Name Case - Calculation attribute
今日份20220719折腾deeplabcut
放图仓库-2(函数图像)
09_ Keyboard events
C and pointer Chapter 18 runtime efficiency 18.3 runtime efficiency
About no module named'django.db.backends.mysql'
转置卷积相关








