Search Unity

[stopped]

Discussion in 'Scripting' started by T0byCat, Oct 19, 2020.

  1. T0byCat

    T0byCat

    Joined:
    Aug 17, 2020
    Posts:
    14
    So for a simple little weapon viewmodel, i wrote this code to add offset to the rotation while still applying the slerp with the offset. I can do this with transform (not slerp however) fine but not rotation.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class followcam : MonoBehaviour
    6. {
    7.     public Transform targetcamera;
    8.     public Vector3 offset;
    9.     public Quaternion rotationOffset;
    10.     public float rotationSpeed = 30f;
    11.  
    12.     void Update()
    13.     {
    14.         transform.position = targetcamera.position + offset;
    15.         transform.rotation = Quaternion.Slerp(transform.rotation, targetcamera.rotation, rotationSpeed * Time.deltaTime);
    16.         // Trying to add RotationOffset to the new result
    17.  
    18.     }
    19. }
    20.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    It's generally helpful to post what you tried. Makes it easier to figure out where someone went wrong.

    My guess is that you were trying to combine the rotations with a + sign. To combine two Quaternions into a single rotation, one multiplies them.

    Note that multiplication of Quaternions is not commutative; A * B is not the same as B * A. The second rotation is applied relative to the reference frame of the first. For instance, if your object's current orientation is R, and dY is a rotation around the Y axis, then setting your rotation to (dY * R) will rotate the object around the global Y axis, while setting your rotation to (R * dY) will rotate the object around its own Y axis.
     
  3. T0byCat

    T0byCat

    Joined:
    Aug 17, 2020
    Posts:
    14

    Okay, so if you view the attached video, when the game is stopped; i want it to slerp to that position, but when i start the game, i get the wanted result execpt for the rotation being weird, i want to apply an offset to the finished result, i tried doing
    targetcamera.rotation * rotationoffset
    and vice versa, but to no avail.