Rotate object around another object C#

I’m building a snowball fight game - part of it involves a snowman spawning and throwing a snowball towards the ‘target’ (the HTC vive headset camera).

I have written a script to spawn the snowman in a random location (within range) around the ‘target’ and rotate him to face the ‘target’. When the snowman spawns the animation begins where he throws a snowball, however the snowball in the animation is only present until the point of release, therefore I have included in the script the spawning of a new snowball which is thrown towards the ‘target’.

The issue is that I cannot get the snowball to spawn in the correct place as when I translate or rotate the snowball it rotates about the local coordinate system of the snowball, so it always spawns to one side of the snowman (i.e. the left hand side) no matter which direction he is facing. Can someone help me figure out how to get the snowball to rotate about the snowman’s local coordinate system please?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
    public float minDist;
    public float maxDist;
    public float vHeight = 0.01f;
    public GameObject Snowman;
    public Transform target;
    public static Vector3 spawnPos;
    public float minTime = 3.0f;
    public float maxTime = 9.0f;
    bool isSpawning = false;
    public GameObject Snowball;
    Vector3 SbSpawnAlter;
    Vector3 SbSpawnTrans;
    Vector3 SbSpawnPos;
  
  // Wait a set number of seconds, then spawn a new game object
    // This function currently uses global vars to pass public properties

    IEnumerator SpawnObject(float seconds, GameObject SpawnMe)
    {
        Debug.Log("Waiting for " + seconds + " seconds");
        yield return new WaitForSeconds(seconds);
        float xPos, yPos, sDir, sDist;
        Vector3 spawnPos;
        GameObject curSpawn;
        GameObject SbSpawn;
        Vector3 newDir;
        Vector3 targetDir;
        Vector3 ballpos;
        Vector3 ballvec;

        float SbSecs = 6.09f;

        // Game object will be 
        sDir = UnityEngine.Random.Range(0, 2 * Mathf.PI);
        sDist = UnityEngine.Random.Range(minDist, maxDist);
        xPos = (Mathf.Cos(sDir) * sDist) + target.position.x;
        yPos = (Mathf.Sin(sDir) * sDist) + target.position.y;
        spawnPos = new Vector3(xPos, vHeight, yPos);
       
 //Instantiate new object
        curSpawn = Instantiate(SpawnMe, spawnPos, Quaternion.identity);

        // Rotate snowman towards target

        targetDir = target.position - curSpawn.transform.position;
        newDir = Vector3.RotateTowards(transform.forward, targetDir, 6.0f, 0.0F);
        Debug.DrawRay(transform.position, newDir, Color.red);
        curSpawn.transform.rotation = Quaternion.LookRotation(newDir);
        curSpawn.transform.Rotate(0, 180, 0);
        
        isSpawning = false;

        //Spawn snowball 6.2secs later
        Debug.Log("Waiting for " + SbSecs + seconds + " seconds");
        yield return new WaitForSeconds(SbSecs);

        ballvec = new Vector3(-1f, 1f, 0f);
        ballpos = Snowman.transform.InverseTransformPoint(ballvec);
        SbSpawn = Instantiate(Snowball, (curSpawn.transform.position + ballpos), curSpawn.transform.rotation);
        SbSpawn.transform.Rotate(0, 180, 0);
        SbSpawn.transform.Rotate(-20, 0, 0);
        SbSpawn.GetComponent<Rigidbody>().AddForce(SbSpawn.transform.forward * 1000.0f);
  
    }

    void Update()
    {

        if (!isSpawning)
        {
            isSpawning = true; //Yep, we're going to spawn
            StartCoroutine(SpawnObject(UnityEngine.Random.Range(minTime, maxTime), Snowman));
        }
    }
}

I don’t know if you noticed, but instead of doing:

GameObject SbSpawn;
SbSpawn = Instantiate(prefab, transform, rotation);

You should do:

GameObject SbSpawn;
SbSpawn = Instantiate(prefab, transform, rotation) as GameObject;

If you don’t do as GameObject, the script will give you an error. I think that’s why it doesn’t work.

Managed to use the position of the snowball in the animation before it disappears to spawn the snowball in the correct position :slight_smile: just took a while and a whole new script o.O