Search Unity

Rotate cube around center

Discussion in 'Scripting' started by CodeWurm, Mar 16, 2019.

  1. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    I'm trying to script my cube to rotate on the x-as around himself towards the mouse position.
    But I'm getting an error: error CS1503: Argument 1: cannot convert from 'UnityEngine.Vector3' to 'float'

    What I understand is the value given in angle_vector is wrong, but I didn't exactly understand what to change.

    Code (CSharp):
    1. // store mouse position
    2.         Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition
    3.             + Vector3.right * 10f);
    4.  
    5.         var angle_vector = new Vector3(mouseWorldPosition, 0, 0);
    6.         transform.Rotate(angle_vector * rotate, Space.Self);
     
  2. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    I think this is wrong but I don't know how to fix it
    angle_vector * rotate
     
  3. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    I don't think that's that problem there is an red line under mouseWorldPosition.
     
  4. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    angle_vector has 3 coordinates + 0 + 0. That makes it Vector5 ?
     
  5. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    I guess I just need to accept that I suck at this.
     
  6. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    I tried some new things out but nothing seems to work.
    Code (CSharp):
    1.  // Store mouse position
    2.         Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition)
    3.             - transform.position;
    4.  
    5.         transform.Rotate(mouseWorldPosition, 0, 0);
    6.  
    7.         // Create quaternion
    8.         //Quaternion rotation = Quaternion.LookRotation(mouseWorldPosition);
    9.         //transform.rotation = Quaternion.Lerp(transform.rotation, rotation, speed * Time.deltaTime);
     
  7. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    Try
    transform.Rotate ( mouseWorldPosition, Space.World);
     
  8. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    The same effect, the cube is just floating through space. But I found a script that works the way I want, I looked at it and it makes sense.