- Home /
Why nothing happens???
Hello! I have made a script that makes an empty gameObject looks at a target. Then I want the y eulerAngle rotation to be applied to an other gameObject. So the line is:
gunBody.eulerAngles.y = aimAssist.eulerAngles.y;
This a Coroutine that is called from at an other line of the script... The problem is that nothing happens. The empty GameObject(aimAssist) rotates but the gunBody does nothing. Also the look at line is:
aimAssist.LookAt(player.position);
Please can somebody help me??? I've tried everything I could think of!!!
Are you attempting to link information between two scripts?
Answer by cgeopapa · Jan 26, 2015 at 08:59 AM
I found the problem... Looks like the way I refere to the transform of the object at the function Start was wrong:
gunBody = GameObject.Find("GunBody").transform;
But, why?
Here is the whole script:
#pragma strict
//General
var health: float;
var playerDist: float;
var player: Transform;
var spoted: boolean = false;
//Attack
var gunBody: Transform;
var gun: Transform;
var aimAssist: Transform;
var aimAssistRot: float;
var rotSpeed: float;
var canShoot: boolean = false;
var bullet: GameObject;
var bulletSpeed: float;
var smoke: ParticleSystem;
var tail: ParticleSystem;
//Chase
var agent: NavMeshAgent;
function Start(){
tail = GameObject.Find("BulletSmoke").particleSystem;
smoke = GameObject.Find("ShootSmoke").particleSystem;
gunBody = GameObject.Find("GunBody").transform;
gun = GameObject.Find("Gun").transform;
aimAssist = GameObject.Find("AimAssist").transform;
agent = GetComponent.<NavMeshAgent>();
}
function FixedUpdate () {
aimAssist.LookAt(player.position);
health = GetComponent(EnemyHealth).health;
playerDist = Vector3.Distance(player.position, transform.position);
if (health > 0){
if(playerDist < 800)
Attack();
else if(spoted)
Chase();
if(playerDist > 800 && !spoted)
Idle();
if(playerDist && !spoted && Time.time > 1)
ReSpawn();
}
}
function Attack(){
spoted = true;
StartCoroutine("Aim");
if(canShoot){
canShoot = false;
smoke.Play();
bullet = new GameObject("Bullet");
bullet.AddComponent("Rigidbody");
bullet.AddComponent("SphereCollider");
bullet.transform.position = gun.position;
bullet.rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
tail.SendMessage("Attach", SendMessageOptions.DontRequireReceiver);
bullet.AddComponent("Bullet");
bullet.rigidbody.freezeRotation = true;
bullet.rigidbody.velocity = gun.TransformDirection(Vector3.down * bulletSpeed);
StartCoroutine("Reload");
}
}
function Aim(){
gunBody.eulerAngles.y = aimAssist.eulerAngles.y;
//gun.eulerAngles.x = aimAssist.eulerAngles.x;
}
function Reload(){
yield WaitForSeconds(5);
canShoot = true;
}
function Chase(){
agent.SetDestination(player.position);
if(Time.time > 5)
Lost();
}
function Lost(){
spoted = false;
}
function Idle(){
transform.position = transform.position;
}
function ReSpawn(){
if(playerDist > 700)
transform.position = new Vector3(Random.Range(0, 10000), 0, Random.Range(0, 10000));
else
gameObject.SetActive(true);
}
Answer by Kacheek · Jan 25, 2015 at 06:11 PM
try
gunBody.eulerAngles = new Vector3(gunBody.eulerAngles.x,aimAssist.eulerAngles.y,gunBody.eulerAngles.z);
edit: sorry i had a typing error in it ! i fixed it already
Nothing! The same thing! I can't understand... it's like skipping the line!!
could you just copy paste your whole script in here ? that way we can make sure if it really is the command itself.
It could also be that you maybe put it in a if state that doesnt get true.... or maybe you've accidently put // in front of it ? or well a lot of other things could happen but this seems to be just a small mistake like a typing error or something similar
Your answer
![](https://koobas.hobune.stream/wayback/20220613181505im_/https://answers.unity.com/themes/thub/images/avi.jpg)