how to use look at for instantiate object.
i have used look at
in this below code in use LOOKAT for normal object
using UnityEngine;
using System.Collections;
public class lkk : MonoBehaviour
{
public Transform look_at;
void Update()
{
transform.LookAt(look_at);
}
}
In this script how to assign look at for the instantiate objects(instantiating object)
this is the instantiate script
using UnityEngine;
using System.Collections;
public class lkk : MonoBehaviour
{
public Rigidbody ball_prb;
public Transform outs;
if(Input.GetButtonDown("Fire1"))
{
Rigidbody ball_instance;
ball_instance=Instantiate(ball_prb,outs.position,outs.rotation)as Rigidbody;
ball_instance.AddForce(outs.forward*1200);
}
First off, you can’t name 2 scripts with the same name. It would have thrown an error in Unity.
Secondly, you can’t put your input outside of functions. It should have thrown another error in Unity.
Next, to answer your question, you can create a variable to reference the first script and just pass the variable ball_instance as a Transform into your variable look_at inside your first script.
// In your second script
public lkk_first myScript;
public Rigidbody ball_prb;
public Transform outs;
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
Rigidbody ball_instance; // You should create this variable outside of the function but I'm just leaving it
ball_instance=Instantiate(ball_prb,outs.position,outs.rotation)as Rigidbody;
ball_instance.AddForce(outs.forward*1200);
myScript.look_at = ball_instance.transform;
}
}
You could either attach a component to the ball_instance object you created, such as your llk component, or you could access the transform through the gameObject of the Rigidbody.