Question by
wongchris · Feb 02, 2020 at 08:58 AM ·
unity 2drotate objectshootlookattarget
How to make a boss who can shot toward an object, while the boss can do motion like the Earth
Now I have two 2D Objects. One is the boss, the other one is the rotate center point. And I also have two balls, which are red_ball and blue_bell. Here is what I want to make:
The boss will move like the Earth (boss) to the Sun (rotate center point)
The boss alway toward the rotate center point
The boss will shot red_ball in the first rotate circle. And shot blue_ball in the second circle.
Here is the script I have(all are under the boss)
[Header("Projectile Settings")]
public int numberOfProjectiles; // Number of projectiles to shoot.
public float projectileSpeed; // Speed of the projectile.
public GameObject ProjectilePrefab; // Prefab to spawn.
public Transform rotationCenter;
public float rotationSpeed;
public bool clockwise = true;
[Header("Private Variables")]
private Vector3 startPoint; // Starting position of the bullet.
private const float radius = 1F; // Help us find the move direction.
public static bool shot_bool = false;
[SerializeField]
float angelarSpeed = 100f;
float rotation_angle = 0f;
// Update is called once per frame
void Update()
{
if (shot_bool == true)
{
startPoint = transform.position;
shot_to_center();
shot_bool = false;
}
rotation_angle = rotation_angle + Time.deltaTime * angelarSpeed;
Debug.Log("rotation_angle " + rotation_angle);
}
public static void shot()
{
shot_bool = true;
}
public void shoot() //for the animation event
{
shot_bool = true;
}
// Spawns x number of projectiles.
//-----------------------------------working------------------------------------------------
private void shot_to_center()
{
float angleStep = 360f;
float angle = 0f;
// Direction calculations.
float projectileDirXPosition = startPoint.x + Mathf.Sin((rotation_angle * Mathf.PI) / 180) * radius;
float projectileDirYPosition = startPoint.y + Mathf.Cos((rotation_angle * Mathf.PI) / 180) * radius;
// Create vectors.
Vector3 projectileVector = new Vector3(projectileDirXPosition, projectileDirYPosition, 0);
Vector3 projectileMoveDirection = (projectileVector - startPoint).normalized * projectileSpeed;
// Create game objects.
GameObject tmpObj = Instantiate(ProjectilePrefab, startPoint, Quaternion.identity);
tmpObj.GetComponent<Rigidbody2D>().velocity = new Vector3(projectileMoveDirection.x, projectileMoveDirection.y, 0);
// Destory the gameobject after 10 seconds.
Destroy(tmpObj, 10F);
angle += angleStep;
}
//-----------------------------------no use now------------------------------------------------
private void SpawnProjectile(int _numberOfProjectiles)
{
float angleStep = 360f / _numberOfProjectiles;
float angle = 0f;
for (int i = 0; i <= _numberOfProjectiles - 1; i++)
{
// Direction calculations.
float projectileDirXPosition = startPoint.x + Mathf.Sin((angle * Mathf.PI) / 180) * radius;
float projectileDirYPosition = startPoint.y + Mathf.Cos((angle * Mathf.PI) / 180) * radius;
// Create vectors.
Vector3 projectileVector = new Vector3(projectileDirXPosition, projectileDirYPosition, 0);
Vector3 projectileMoveDirection = (projectileVector - startPoint).normalized * projectileSpeed;
// Create game objects.
GameObject tmpObj = Instantiate(ProjectilePrefab, startPoint, Quaternion.identity);
tmpObj.GetComponent<Rigidbody2D>().velocity = new Vector3(projectileMoveDirection.x, projectileMoveDirection.y,0);
// Destory the gameobject after 10 seconds.
Destroy(tmpObj, 10F);
angle += angleStep;
}
}
It is the RotateMove.
[Header("RotateMove")]
[SerializeField]
Transform rotationCenter;
[SerializeField]
float rotationRadius = 2f, angelarSpeed = 10f;
float posX, posY, rotation_angle = 0f;
float angle = 0f;
public float rotationSpeed;
public bool clockwise = true;
Transform myTrans;
Vector3 myPos;
Vector3 myRot;
void Awake()
{
myTrans = transform;
myPos = myTrans.position;
myRot = myTrans.rotation.eulerAngles;
StartCoroutine(fight());
}
//-----------------------------------working------------------------------------------------
public IEnumerator fight()
{
while(true)
{
FireBullet.shot();
yield return new WaitForSeconds(1f);
}
}
private void Update()
{
posX = rotationCenter.position.x + Mathf.Cos(rotation_angle) * rotationRadius;
posY = rotationCenter.position.y + Mathf.Sin(rotation_angle) * rotationRadius;
transform.position = new Vector2(posX, posY);
rotation_angle = rotation_angle + Time.deltaTime * angelarSpeed;
if (rotation_angle >= 360f)
rotation_angle = 0f;
angle = myTrans.eulerAngles.magnitude * Mathf.Deg2Rad;
//rotate object Right & Left
if (clockwise == true)
{
myRot.z -= rotationSpeed;
}
if (clockwise == false)
{
myRot.z += rotationSpeed;
}
myTrans.rotation = Quaternion.Euler(myRot);
}
It is look to center
[Header("Look_to_the_center")]
public Transform Target;
public float TurnSpeed = 50.00F;
private Transform myTransform;
public void Start()
{
myTransform = GetComponent<Transform>(); // Cache own Transform component
}
public void Update()
{
var targetDirection = Target.position - myTransform.position;
targetDirection.z = 0.00F; // Lock global y-axis
var targetRotation = Quaternion.LookRotation(targetDirection);
var deltaAngle = Quaternion.Angle(myTransform.rotation, targetRotation);
if (deltaAngle == 0.00F)
{ // Exit early if no update required
return;
}
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,targetRotation,TurnSpeed * Time.deltaTime / deltaAngle);
}
Some above script I just copy from the internet as I am very new in unity. But I will try to learn it.
Comment
Answer by wongchris · Feb 02, 2020 at 05:32 PM
I solved the problem of these code.
public class Look_to : MonoBehaviour
{
[Header("Look_to_the_center")]
public Transform Target;
Quaternion XLookRotation2D(Vector3 right)
{
Quaternion rightToUp = Quaternion.Euler(0f, 0f, 90f);
Quaternion upToTarget = Quaternion.LookRotation(Vector3.forward, right);
return upToTarget * rightToUp;
}
private void Update()
{
Vector3 relativePos = Target.position - transform.position;
Quaternion angle = XLookRotation2D(relativePos);
transform.rotation = Quaternion.Lerp(transform.localRotation, angle, Time.deltaTime);
}
private IEnumerator shot_to_center()
{
yield return new WaitForSeconds(0.01f);
GameObject tmpObj = Instantiate(ProjectilePrefab, startPoint, Quaternion.identity);
foreach (GameObject f in backgroundObjects1)
{
Vector3 shoot = (rotationCenter.position - transform.position).normalized;
yield return new WaitForSeconds(7.5f);
tmpObj.GetComponent<Rigidbody2D>().AddForce(shoot * 100f);
Destroy(tmpObj, 10F);
}
}