hi i have an script her for my rocket so that when it launched(cloned) it takes the target from the ship. and then go for it, but my script does not work at all, when it takes the target so i need help
using System.Collections;
public class Rocketscript : MonoBehaviour {
public float RocketSpeed;
public float RotationSpeed;
public float DetnateTime;
public float Damage;
public GameObject Target;
private Vector3 TargetDir;
private float stepR;
private Vector3 newDir;
public PlayerShip Playership;
// Use this for initialization
void Awake() {
//this is where i want the racket to get it's target.
Playership = GetComponent<PlayerShip>();
Target = Playership.PlayerTarget;
}
void Start () {
Destroy(gameObject, DetnateTime);
}
// Update is called once per frame
void Update () {
if (Target != null)
{
transform.Translate(0, 0, RocketSpeed * Time.deltaTime);
TargetDir = Target.transform.position - transform.position;
stepR = RocketSpeed * Time.deltaTime;
newDir = newDir = Vector3.RotateTowards(transform.forward, TargetDir, stepR, 0.0f);
}
if (Target == null) {
transform.Translate(0, 0, RocketSpeed * Time.deltaTime);
}
}
}
also can you explain to me how the GetComponent works, cuz i have no general idea of it and the unity’s help does not have any thing good. thanks
GetComponent gets the component (Transform, mesh renderer, your own scripts…) from your rocket object. You have to use GameObject.Find(“Player”).GetComponent() to get your script, assuming the object is called Player and the only one with that name in the scene.
There are other methods (setting the target on instantiation in the player script) but you should understand the basics first.
well i did that and changed the line
Playership = GetComponent();
with
GameObject.Find(“Player”).GetComponent ();
and yet it still send an error:
NullReferenceException: Object reference not set to an instance of an object
Rocketscript.Awake () (at Assets/Materials/Bullet/Rocketscript.cs:15)
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
PlayerShip:Update() (at Assets/Materials/Player/PlayerShip.cs:153)
Well, first you should probably use playerShip = GameObject.FindGameObjectWithTag(“Player”);
Then set the root of the player’s GameObject tag to be Player. This way, you can name the actual Game Object whatever you want, and if it’s accidentally renamed (like Player(Clone)) everything still works.
Next, what exactly is “PlayerShip”? Is that a script? If so, you’re trying to set a script/component as a vector3. Instead, you want something more like:
ok thanks for your tips so far. however while my rockets do now have the target from the player ship. BUT most of the rockets that are launched does not rotate toward target, and simply move in a straight line like a bullet froward, i either assume that while they do have a target for some reasons the beehive like they don’t have a target(consider the target null) or there is something with the rotation commands,either ways most of bullets go in an straight line and the rest is just pointlessly circle around for no reasons, and only a few of them rotate toward the target,
here is the script with the changes
using UnityEngine;
using System.Collections;
public class Rocketscript : MonoBehaviour {
public float RocketSpeed;
public float RotationSpeed;
public float DetnateTime;
public float Damage;
public GameObject Target;
private Vector3 TargetDir;
private float stepR;
private Vector3 newDir;
public PlayerShip Playership;
void Awake() {
//taking the target:
Playership=GameObject.Find("Player").GetComponent<PlayerShip>();
Target = Playership.PlayerTarget;
}
void Start () {
Destroy(gameObject, DetnateTime);
}
void Update () {
if (Target == null)
{
//go straight if no target was there(or target is destroyed)
Debug.Log("we have no target");
transform.Translate(0, 0, RocketSpeed * Time.deltaTime);
}
else
{
Debug.Log("we have the target");
transform.Translate(0, 0, RocketSpeed * Time.deltaTime);
TargetDir = Target.transform.position - transform.position;
stepR = RocketSpeed * Time.deltaTime;
newDir = Vector3.RotateTowards(transform.position , TargetDir, stepR, 0.0f);
}
}
}
But I can see that your code is convoluted. You’re trying to find the player by name then accessing a component on the player, I assume for a variable that you’re storing the transform in. My previous suggestion allows you to get the transform directly without accessing the component.
I implemented what you’re trying to do (skip to 30 sec):
sorry for all the troubles that i made for you s3dilition and suddoha so far, but the point is, since the are a lot of things that are going to work just like the Rocket script that i mentioned in my game, and also not to mention i am going to have different types of missiles that might script for most of them be based on this simple one, so i really need something neat and simple to work with, and something which i understand, therefore i rather do at least the main part by myself the script is not perfect and it is just for a simple rocket, i more like to know how to fix the issue with the tracking system of my rocket other than change the whole thing at this point, so can you guys help me to fix my own script, i might be a dummy so i think i learn much better if i get my mistakes
now can you tell where things went wrong?and how can i fix that part with out having to fix the entire script
PlayerShip is a script which handle all the things related to the player, and it has the target which player took, now how can i give the target to the missile right at the moment it launched and make the rocket to stick with it,?
We cannot help you unless you make some changes and tell us what happened. I’ve given you 3 full angles to explore, including a fully working script. If you don’t apply something to the script, at least debugging and telling us what is failing, we can’t help you.
OKAY! No i have to say so sorry to all of you people cuz now, after like 3 hour wasting your time, i finally realized that i accidentally removed the part which supposed to made the rotation happen, and therefor there was really nothing in that script which could guided my rockets at all,
i think all those rockets that i mentioned circling around and even rotating toward the target were just objects that collided with other rockets.
again so sorry for all that, i add the : “transform.rotation = Quaternion.LookRotation(newDir);” to my script which solved the problem
beside the fact that i don’t know how to make my rockets and bullets ignore colliding with each other, which is the only left mystery that i have to ask you now (i really don’t know how) and sorry for that foolish mistake of mine
(i want my bullets to ignore collision with all the objects tagged either as rocket or bullet)
But using this method, will objects go through the other ? I now it’s possible with layer, but I thought that if 2 objects are on the same layer or in layers that can interact, they will collide, even if u disable the damage or anithing with some statement.
well my Bullet and Rocket Scripts so far which have the damaging script applied to them are dealing damage based on the target they hit.
but the issue is my bullets and rockets are all carrying the Rigidbody which as you know, means that bullets are going to collision with all objects in game with the Rigidbody which is every objects in game
now here is a sample of my bullet script.
i am thinking of making a list here for my bullets and rockets and totally all the things that my rocket should ignore colliding and then i want my rocket/bullet to make collision with everything else and do the damaging script.
i tried making the list myself, but there is an issue when i trying to add both objects with tag bullet and with tag rocket, somewhat the script just send an error and when i ignore it just list the objects with bullet tag,(that script had so many other errors and proven that i don’t really know much about using the lists )
using UnityEngine;
using System.Collections;
public class Bullet1 : MonoBehaviour
{
public int damage = 3;
void Start()
{
Destroy(gameObject, 3
);
}
void Update()
{
}
void OnCollisionEnter ( Collision collision ){
Destroy (gameObject,0);
collision.transform.SendMessage("Hitted",damage,SendMessageOptions.DontRequireReceiver);
}
If you disable collisions between the Bullet-layer and the Bullet-layer, there will occur no collisions (… ). You won’t have to give your bullets specific tags and you don’t have to check them and the physics engine has a bit less work.