- Home /
Lookat function working before implementing Multiplayer / Networking.
My player object and script are working and flying around with networking just fine. My bullets are instantiating, moving forward which their own script tells them to, but not doing their "LookAt Target" thing on their start frame.
I even defined the instantiate inside a variable and am using NetworkServer.Spawn as the unity basic multiplayer setup manual says.
What am I missing?
Here is the code. Update is running, Start does not seem to be.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class shotmover : NetworkBehaviour {
public float speed;
public bool hasTarget;
public Transform target;
public Player player;
public Transform initpos;
public Vector3 targetahead;
// public AudioSource pew;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
public float offsetz;
public float offsety;
public float offsetx;
public float VertOffset;
public float FollowHeight;
public float FollowDistance;
public float showme;
public float showme2;
public Rigidbody rb;
bool flip;
// Use this for initialization
void Start() {
//RpcLockon ();
}
// Update is called once per frame
void FixedUpdate () {
// if (!hasTarget)
gameObject.transform.Translate (Vector3.forward * speed);
//rb.AddForce(transform.forward * speed);
if (hasTarget) {
Debug.DrawLine (gameObject.transform.position, targetahead);
Debug.DrawLine (target.transform.position, targetahead);
Debug.DrawLine (initpos.transform.position, targetahead);
}
// if (hasTarget) {
// Vector3 targetPosition = target.TransformPoint (new Vector3 (VertOffset, FollowHeight, FollowDistance));
// transform.position = Vector3.SmoothDamp (transform.position, targetPosition, ref velocity, smoothTime);
// transform.rotation *= Quaternion.Euler (offsetz, offsety, offsetx);
// }
}
public void Destroyshot(){
Destroy (gameObject, 2f);
}
[ClientRpc]
void RpcLockon(){
rb = gameObject.GetComponent<Rigidbody> ();
Destroyshot ();
hasTarget = player.seesTarget;
if (hasTarget) {
target = player.locktarget;
initpos = target.transform;
showme2 = (Vector3.Distance (transform.position, target.position));
//if (showme > showme2)
//targetahead += target.position + target.forward * (((Vector3.Distance(transform.position, target.position)) / 100) * (player.locktarget.gameObject.GetComponent<Rigidbody> ().velocity.magnitude));
targetahead += target.position + (target.GetComponent<Rigidbody>().velocity) + player.locktarget.gameObject.GetComponent<Rigidbody> ().velocity/showme2*(Mathf.Sqrt((Mathf.Sqrt(showme2)/speed) * (Mathf.Pow(Mathf.Sqrt(player.locktarget.gameObject.GetComponent<Rigidbody> ().velocity.magnitude), rb.velocity.magnitude)+Mathf.Pow(showme2, Mathf.Sqrt(showme))-Mathf.Sqrt(showme)+player.locktarget.GetComponent<Rigidbody>().velocity.sqrMagnitude-speed)));
//targetahead += target.position - player.locktarget.gameObject.GetComponent<Rigidbody> ().velocity * (Mathf.Sqrt((Mathf.Sqrt(showme2)) * (Mathf.Pow(player.locktarget.gameObject.GetComponent<Rigidbody> ().velocity.magnitude, rb.velocity.magnitude)+Mathf.Pow(showme2, Mathf.Sqrt(showme))+Mathf.Sqrt(showme)+Mathf.Sqrt(player.locktarget.GetComponent<Rigidbody>().velocity.sqrMagnitude))));
transform.LookAt (targetahead);
Debug.Log ("Target velocity:" + player.locktarget.gameObject.GetComponent<Rigidbody> ().velocity.magnitude.ToString ());
showme = (Vector3.Distance(targetahead, target.position));
Debug.Log ("Distance:" + showme2.ToString ());
Debug.Log ("Targeting forward ahead:" + showme.ToString ());
//rb.AddForce(transform.forward * speed);
//gameObject.transform.Translate (Vector3.forward * speed);
}
}
}
Everything in RpcLockon was originally in Start but neither way is working
I would also like to include my player script. My player grabs "locktarget" with a trigger collider. Those trigger collisions dont seem to be happening anymore. What's up? using UnityEngine; using System.Collections; using UnityEngine.Networking;
public class Player : NetworkBehaviour {
public float shotTime;
public float shotCooldown;
CharacterController cc;
public float speed;
public float turnspeed;
public ParticleSystem engineparticle;
public float boost;
public float boostHolder;
public bool isBoosting;
public Transform shotpos;
public GameObject shot;
public Rigidbody rb;
public Vector3 forward;
public bool onGround;
public float vfloat;
public float pitch;
public float yaw;
public float roll;
public bool seesTarget;
public Transform locktarget;
public PlayerHealth php;
NetworkIdentity nid;
//public float
//public float
//public float
// Use this for initialization
void Start ()
{
shotTime = shotCooldown;
nid = GetComponent<NetworkIdentity> ();
onGround = false;
forward = Vector3.forward;
engineparticle.Stop ();
// displaceHolder = displaceAmount;
cc = GetComponent<CharacterController> ();
// lengthpos = new Vector3 (transform.position.x, transform.position.y, transform.position.z - displaceAmount);
}
void Update (){
shotTime -= .1f;
if (!isLocalPlayer) {
return;
}
pitch = -Input.GetAxis ("Vertical");
yaw = -Input.GetAxis ("Horizontal");
roll = Input.GetAxis ("Roll");
}
// Update is called once per frame
void FixedUpdate () {
if (!isLocalPlayer) {
return;
}
//Always move the player forward, relative to its rotation.
//rb.AddRelativeForce(Vector3.up * (speed + boostHolder));
//rb.MovePosition(Vector3.forward * (speed + boostHolder));
/*
transform.Translate(forward * (speed + boostHolder));
transform.Rotate(vfloat * turnspeed, Input.GetAxis("Horizontal") * turnspeed, ((Input.GetAxis("Horizontal") / 3) * -1) + Input.GetAxis("Roll") * -turnspeed);
*/
if (!Input.GetKey(KeyCode.Space)) {
rb.AddRelativeForce (Vector3.up * (speed + boostHolder));
}
//transform.Rotate(pitch * turnspeed, roll * turnspeed, ((roll / 3) * -1) + yaw * -turnspeed, relativeTo:Space.Self);
rb.AddRelativeTorque (pitch * turnspeed, -roll * turnspeed,((roll / 3)+ yaw) * -turnspeed);
//rb.AddTorque (Input.GetAxis("Vertical") * turnspeed, Input.GetAxis("Horizontal") * -turnspeed,0);
//transform.Rotate(vfloat * turnspeed, Input.GetAxis("Horizontal") * turnspeed, ((Input.GetAxis("Horizontal") / 3) * -1) + Input.GetAxis("Roll") * -turnspeed);
//Rotate left and right with a and d, rotate up and down with w and s.
/* if (Input.GetKey (KeyCode.S) && !onGround)
vfloat = -1f;
if (Input.GetKey (KeyCode.W))
vfloat = 1f;
else
vfloat = 0f;*/
//while you press shift,
if (Input.GetKey (KeyCode.LeftShift)) {
isBoosting = true;
engineparticle.gameObject.SetActive (true);
} else {
isBoosting = false;
engineparticle.gameObject.SetActive (false);
}
if (isBoosting)
boostHolder = boost;
else
boostHolder = 0;
//If the player press space,
if (Input.GetMouseButton(1) && shotTime <= 0f) {
CmdFire ();
shotTime = shotCooldown;
//NetworkServer.Spawn(Coneshot);
//Coneshot.GetComponent<shotmover>().Start
}
//instantiate a shot, at shotpos.
}
void OnTriggerStay(Collider other){
Debug.Log ("Trigger enter");
//Collide with a certain thing to trigger an instantiation method.
//if (other.CompareTag ("terrain")) {
//forward += new Vector3 (0, 1, 0);
//onGround = true;
//}
if (other.CompareTag ("enemy") && !seesTarget) {
other.GetComponent<FlightAI> ().isTargeted = true;
other.GetComponent<FlightAI> ().targetind.gameObject.SetActive (true);
seesTarget = true;
locktarget = other.gameObject.transform;
Debug.DrawLine (transform.position, locktarget.position);
Debug.Log ("See target");
//Debug.Log ("Target at distance" + Vector3.Distance (transform.position, locktarget.position).ToString ());
}
}
void OnTriggerExit(Collider other){
//if (other.CompareTag ("terrain")) {
////anim.SetBool ("hitground", false);
//forward = Vector3.forward;
//onGround = false;
//php.hp -= 10f;
// }
if (other.CompareTag ("enemy")) {
other.GetComponent<FlightAI> ().isTargeted = false;
other.GetComponent<FlightAI> ().targetind.gameObject.SetActive (false);
seesTarget = false;
locktarget = null;
Debug.Log ("Lost target");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612104836im_/https://answers.unity.com/themes/thub/images/avi.jpg)