0
\$\begingroup\$

I am working on Unity Networking and following this Tanks Networking tutorial:

https://www.youtube.com/watch?v=V6wEvT6G92M

Problem is that the keyboard inputs move all the tanks in the scene. It is not detecting the local tank. And when I run the game all tanks shows the same movement but it is not giving any error. I have followed all steps in the tutorial and searched all solutions on the google. This step in Tutorial is at 11:48.

Implemented Code is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.Networking;


public class TankController : NetworkBehaviour {

// Use this for initialization

[Header("Movement Variables")]
[SerializeField] float movementSpeed = 5.0f;
[SerializeField] float turnSpeed = 45.0f;

[Header("Camera Position Variables")]
[SerializeField] float cameraDistance = 16f;
[SerializeField] float cameraHeight = 16f;

Rigidbody localRigidBody;
Transform mainCamera;
Vector3 cameraOffset;


void Start () {
    if (!isLocalPlayer)
    {
        Destroy(this);
        return;
    }
        localRigidBody = GetComponent<Rigidbody>();
        cameraOffset = new Vector3(0f, cameraHeight, -cameraDistance);
        mainCamera = Camera.main.transform;
        MoveCamera();


}

// Update is called once per frame
void FixedUpdate () {


        float turnAmount = CrossPlatformInputManager.GetAxis("HorizontalUI");
        float moveAmount = CrossPlatformInputManager.GetAxis("VerticalUI");

        Vector3 deltaTranslation = transform.position + transform.forward * movementSpeed * moveAmount * Time.deltaTime;
        localRigidBody.MovePosition(deltaTranslation);

        Quaternion deltaRotation = Quaternion.Euler(turnSpeed * new Vector3(0, turnAmount, 0) * Time.deltaTime);
        localRigidBody.MoveRotation(localRigidBody.rotation * deltaRotation);

        MoveCamera();

}

void MoveCamera() {
        mainCamera.position = transform.position;
        mainCamera.rotation = transform.rotation;
        mainCamera.Translate(cameraOffset);
        mainCamera.LookAt(transform);

}
}

I have also checked the auto create player in the spawn info as suggested in some solutions.

\$\endgroup\$
1
\$\begingroup\$

Use the isLocalPlayer property, as you did in Start(). Use it wherever you want to keep things 'local':

void FixedUpdate () {

    if(this.isLocalPlayer)

        float turnAmount = CrossPlatformInputManager.GetAxis("HorizontalUI");
        float moveAmount = CrossPlatformInputManager.GetAxis("VerticalUI");

        Vector3 deltaTranslation = transform.position + transform.forward * movementSpeed * moveAmount * Time.deltaTime;
        localRigidBody.MovePosition(deltaTranslation);

        Quaternion deltaRotation = Quaternion.Euler(turnSpeed * new Vector3(0, turnAmount, 0) * Time.deltaTime);
        localRigidBody.MoveRotation(localRigidBody.rotation * deltaRotation);

        MoveCamera();

    }
}
\$\endgroup\$
  • \$\begingroup\$ Did not worked. \$\endgroup\$ – Anila Naqvi Jul 10 '17 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.