0

I have a sphere script that on collision it instantiates a cylinder (object3), then once the cylinder appears, the cylinder is supposed to rotate some way, the direction of rotation doesn't matter. But the code I have is not getting the cylinder to rotate. I've tried different lines of code and tried putting the quaternion lines in the update function and in the on collision function but neither way gets the cylinder to rotate. I am not sure if it has to do with my code at this point, or if I have to do something within unity 3d settings. Any suggestions?

public class sphereBehavior : MonoBehaviour
{
    public GameObject object3;
    private Vector3 direction = new Vector3(-1, 0, 0);
    private Vector3 rotateDirection = new Vector3(0, 0, 1);

    // Update is called once per frame
    void Update()
    {
        transform.Translate(direction * Time.deltaTime, Space.World);
        transform.Rotate(rotateDirection, Space.World);

        Quaternion Cylinder = Quaternion.Euler(90,50,69);
        transform.rotation = Quaternion.Slerp(transform.rotation, Cylinder, Time.deltaTime);

    }

    //void OnCollisionEnter(Collision collision) => Instantiate(object3, new Vector3(0, 1, -2), Quaternion.identity);

    void OnCollisionEnter(Collision collision)
    {
        Instantiate(object3, new Vector3(0, 1, -2), Quaternion.identity);
        Destroy(collision.gameObject);
    }

}
2
  • it is supposed to rotate some way What is "it," the cylinder or the object this script is attached to? Because right now you're rotating the object this script is attached to. Commented Sep 15, 2019 at 1:23
  • @Draco18s I updated the post to clarify, but the cylinder is supposed to be instantiated then the cylinder is supposed to start rotation Commented Sep 15, 2019 at 1:31

1 Answer 1

3

In order to rotate the cylinder you need to rotate the cylinder

transform.rotation refers to the object the script is attached to (i.e. your sphere, if the class name is anything to go by). In order to rotate the spawned cylinder you need to:

  1. save a reference to it after spawning it
  2. reference it's transform

Calling a local variable Cylinder is not sufficient, its just a local variable. As such I've renamed it to something more informative.

public class sphereBehavior : MonoBehaviour
{
    public GameObject object3;
    private Vector3 direction = new Vector3(-1, 0, 0);
    private Vector3 rotateDirection = new Vector3(0, 0, 1);
    private GameObject theCylinder;

    // Update is called once per frame
    void Update()
    {
        transform.Translate(direction * Time.deltaTime, Space.World);
        transform.Rotate(rotateDirection, Space.World);

        Quaternion newRotation = Quaternion.Euler(90,50,69);
        theCylinder.transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime); //reference it
    }

    void OnCollisionEnter(Collision collision)
    {
        theCylinder = Instantiate(object3, new Vector3(0, 1, -2), Quaternion.identity); //save a reference
        Destroy(collision.gameObject);
    }
}

Note that this can only reference one spawned cylinder at a time, so it may be more prudent to create a new script that handles the rotation and attach it to the prefab instead.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.