Search Unity

Look At With Relative VectorUP

Discussion in 'Scripting' started by Utopien, Aug 5, 2019.

  1. Utopien

    Utopien

    Joined:
    Feb 15, 2016
    Posts:
    46
    hello Community

    i am trying to Make look at for my character on sphère like a planet so the vector is relative to the char pos and the center of the sphère


    i have a :
    character
    pivotcam
    the camera (witch is parented to the pivot)
    target parented to the camera and translate 10 unit localposition Z




    Code (CSharp):
    1.  
    2.  
    3.             rotationX += Input.GetAxis("Mouse X");
    4.             rotationY += Input.GetAxis("Mouse Y");
    5.             upVector = (owner.position - sphere.position).normalized;
    6.  
    7.  
    8.             Quaternion q_RotXPivotV2Xrot = Quaternion.AngleAxis(rotationX, upVector);
    9.             Quaternion q_RotXPivotV2Yrot = Quaternion.AngleAxis(rotationY, Owner.right);
    10.        
    11.             Quaternion look = Quaternion.LookRotation(Owner.forward, upVector);
    12.             pivotCam.transform.rotation =  look* q_RotXPivotV2Xrot * q_RotXPivotV2Yrot;
    13.  
    14.             Owner.rotation = Quaternion.LookRotation(target.forward, upVector);
    15.  
    16.  

    my code not working any help would be great !
     
    Last edited: Aug 5, 2019
  2. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    Hmmm... Line 14 is the important line here, since that's where you set the rotation that you want. Your upVector variable seems to be set properly with the calculation on Line 5. That's assuming that owner.position is a reasonable pivot point to use, relative to your object and the center of the sphere.

    But actually, I don't think we know much about target.forward from your code snippet. That's probably where your problem is, but I can't say for sure unless you post the full script code.
     
    Utopien likes this.
  3. Utopien

    Utopien

    Joined:
    Feb 15, 2016
    Posts:
    46


    yes sorry i forgot there is no really a code for the target since the target is simply parented to the camera and translate 10 unit localposition Z
     
    ModLunar likes this.
  4. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    Oh I see, thanks for clarifying! You may be able to using vector projection onto a plane (in this case, a tangent plane to the sphere) to orient their forward direction. Perhaps something like this:
    (using the upVector variable from the script above in the original thread post)

    Code (CSharp):
    1. Vector3 tangentForward = Vector3.ProjectOnPlane(new Vector3(0, 0, 1), upVector);
    2. tangentForward.Normalize();
    I just put "new Vector3(0, 0, 1)" in there as an example, but you can change that. Perhaps a direction based on the forward/right local directions of your character. Just make sure, since all of these calculations that you posted in your script so far are in world space, that you replace "new Vector3(0, 0, 1)" with a world-space direction. tangentForward in my example will then also be in world-space. :)
     
    Utopien likes this.
  5. Utopien

    Utopien

    Joined:
    Feb 15, 2016
    Posts:
    46
    i didnt know about projection plane thanks for the tips i will try that right a away