- Home /
Question by
ezecapo99 · Mar 13, 2018 at 08:45 AM ·
networkinganimatorplayermovement script
help animator not acces other gameobject,Animator acces other gameobject?
hello everyone I'm half bad for English, I need your help please I was quietly making a script so I can move the character but I throw the following error:
MissingComponentException: There is no 'Animator' attached to the "Net" game object, but a script is trying to access it. You probably need to add to Animator to the game object "Net". Or your script needs to check if the component is attached before:
movimiento.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movimiento : MonoBehaviour {
//variables
private float animSpeed = 1.5f;
private float back = 2.0f;
private float forward = 7.0f;
private float rotateSpeed = 4.0f;
private float jumpPower = 6.0f;
private float jumpLimit = 1.5f;
public GameObject playerobj;
bool isGrounded = true;
private Animator anim;
private Rigidbody rb;
private CapsuleCollider col;
private Vector3 velocity;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
col = GetComponent<CapsuleCollider>();
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate() {
Transform tranf = playerobj.transform;
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
anim.SetFloat("Speed", v);
anim.SetFloat("Direction", h);
anim.speed = animSpeed;
rb.useGravity = true;
velocity = new Vector3(0, 0, v);
velocity = playerobj.transform.TransformDirection(velocity);
if (v > 0.1)
{
velocity *= forward;
} else if (v < -0.1)
{
velocity *= back;
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.AddForce(Vector3.up * jumpPower, ForceMode.VelocityChange);
anim.SetBool("Jump", true);
}
else
{
anim.SetBool("Jump", false);
}
playerobj.transform.localPosition += velocity * Time.fixedDeltaTime;
playerobj.transform.Rotate(0, h * rotateSpeed, 0);
}
void OnCollisionEnter(Collision collision)
{
Debug.Log("Entered");
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
Debug.Log("Exited");
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
}
network.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SocketIO;
using System.Text.RegularExpressions;
public class network : MonoBehaviour {
public SocketIOComponent socket;
public Movimiento mov;
public Player player;
// Use this for initialization
void Start () {
StartCoroutine(ConnectToServer());
socket.On("USER_CONNECTED", OnUser);
socket.On("PLAY", OnPlay);
}
IEnumerator ConnectToServer()
{
yield return new WaitForSeconds(0.5f);
socket.Emit("conectar");
Dictionary<string, string> data = new Dictionary<string, string>();
Vector3 position = new Vector3(10, 0, 11);
data["position"] = position.x + "," + position.y + "," + position.z;
socket.Emit("PLAY", new JSONObject(data));
}
void OnUser (SocketIOEvent obj)
{
GameObject otherPlater = GameObject.Instantiate(player.gameObject, player.position, Quaternion.identity) as GameObject;
Player otherPlayerCom = otherPlater.GetComponent<Player>();
otherPlater.transform.position = JsonToVecter3(JsonToString(obj.data.GetField("position").ToString(), "\""));
otherPlayerCom.id = JsonToString(obj.data.GetField("id").ToString(), "\"");
}
void OnPlay(SocketIOEvent obj)
{
GameObject ply = GameObject.Instantiate(player.gameObject, player.position, Quaternion.identity) as GameObject;
Player playerCom = ply.GetComponent<Player>();
ply.transform.position = JsonToVecter3(JsonToString(obj.data.GetField("position").ToString(), "\""));
playerCom.id = JsonToString(obj.data.GetField("id").ToString(), "\"");
mov.playerobj = ply;
}
Vector3 JsonToVecter3(string target)
{
Vector3 newVector;
string[] newString = Regex.Split(target, ",");
newVector = new Vector3(float.Parse(newString[0]), float.Parse(newString[1]), float.Parse(newString[2]));
return newVector;
}
string JsonToString(string target, string s)
{
string[] newString = Regex.Split(target, s);
return newString[1];
}
}
wadwadaw.png
(249.7 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612154439im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Alfernative to transform.Move? Unet 1 Answer
Rotate game object and then return to its original rotation 1 Answer
How to display player ID on client sides? 1 Answer
UNET ReplacePlayerForConnection problem 0 Answers
Network Game Player reference 0 Answers