Search Unity

Make a character controller rotate to the same y axis as camera when an input is held?

Discussion in 'Scripting' started by Treasureman, Feb 27, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have an FPS Controller in my game (set up to a mouse orbit script) and I'm having trouble. I need a code that rotates the player to the same y-axis as the camera when the input "Aim" is held. The camera also isn't attached to the player because I'm using my own version of the mouse orbit script where the camera can't be a child of the player.
    Thanks!
     
  2. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    By axis do you mean rotation? As in the character is looking right and the camera is looking forward and you want the character to look in the direction of the camera when aiming? If so:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Character : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     private float aimSpeed;
    7.  
    8.     private void Update()
    9.     {
    10.         if (Input.GetKey(KeyCode.Mouse1)) // For right click (could be Mouse2 I can't test it right now)
    11.             Aim();
    12.     }
    13.     private void Aim()
    14.     {
    15.         Vector3 aimRotation = new Vector3(transform.rotation.x, Camera.main.transform.rotation.y, transform.rotation.z);
    16.         transform.rotation = Vector3.Lerp(transform.rotation, aimRotation, Time.deltaTime * aimSpeed);
    17.     }
    18. }
    Give that a try and let me know if it works.
     
    Last edited: Feb 28, 2015
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Thanks, but I'm not great with Keycodes. I usually use..
    Code (CSharp):
    1. if Input.(GetButton("Aim"))
    How would I change it to that? Would I just replace keycode mouse1 to it?

    By the way, you're right. Mouse1 is right click and mouse 2 is scroll wheel click
     
  4. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    Yeah replace:
    Code (csharp):
    1. if (Input.GetKey(KeyCode.Mouse1))
    with:
    Code (csharp):
    1. if (Input.GetButton("Aim"))
     
  5. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I tried the script and got a ton of errors.

    Assets/AimRotate.cs(15,51): error CS0103: The name `transform' does not exist in the current context

    Assets/AimRotate.cs(15,107): error CS0103: The name `transform' does not exist in the current context

    Assets/AimRotate.cs(15,128): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments

    Assets/AimRotate.cs(15,128): error CS1503: Argument `#1' cannot convert `object' expression to type `float'

    Assets/AimRotate.cs(16,51): error CS0103: The name `transform' does not exist in the current context

    Assets/AimRotate.cs(16,46): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments

    Assets/AimRotate.cs(16,46): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Vector3'

    Assets/AimRotate.cs(16,17): error CS0103: The name `transform' does not exist in the current context
     
  6. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    Forgot to derive from MonoBehaviour (as this script is being attached to a GameObject) I've edited my original code so it should work.
    Code (csharp):
    1. public class Character
    becomes:
    Code (csharp):
    1. public class Character : MonoBehaviour
     
  7. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    That definetly made it go down but 2 of them are still left.

    Assets/AimRotate.cs(16,46): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments

    Assets/AimRotate.cs(16,46): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Vector3'
     
  8. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Try changing the line 16 to this:

    Code (CSharp):
    1. transform.rotation= Quaternion.Lerp(transform.rotation, Quaternion.Euler(aimRotation), Time.deltaTime* aimSpeed);
     
  9. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    This should fix the last of them.
     
  10. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    One more left. I'm sorry I'm havig so much trouble but I not very good with C#. I usually use JavaScript.

    Assets/AimRotate.cs(17,1): error CS8025: Parsing error
     
  11. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    You are probably having problems with your brackets & syntax, check them.
     
    Fyko-chan likes this.
  12. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    It still doesn't work. I don't have any errors, but I do get this... Screenshot (105).png
    Here's the script with all of the changes so it doesn't too confusing...
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Character : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     private float aimSpeed;
    7.    
    8.     private void Update()
    9.     {
    10.         if (Input.GetButton("Aim")) // For right click (could be Mouse2 I can't test it right now)
    11.             Aim();
    12.     }
    13.     private void Aim()
    14.     {
    15.         Vector3 aimRotation = new Vector3(transform.rotation.x, Camera.main.transform.rotation.y, transform.rotation.z);
    16.         transform.rotation= Quaternion.Lerp(transform.rotation, Quaternion.Euler(aimRotation), Time.deltaTime* aimSpeed);
    17.     }}
     
  13. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    "Check to see if the file name and class name match" They should be the same
     
    lineupthesky likes this.
  14. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    They are. how do i fix it
     
  15. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    Notice how the file's name is AimRotate but the class name inside the file is Character. Change Character to AimRotate in the script.
     
  16. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    Here's a JavaScript version of the code. May be easier to work with.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. @SerializeField
    4. private var aimSpeed : float;
    5.  
    6. function Update() {
    7.     if (Input.GetButton("Aim"))
    8.         Aim();
    9. }
    10. function Aim() {
    11.     var aimRotation : Vector3 = Vector3(transform.rotation.x, Camera.main.transform.rotation.y, transform.rotation.z);
    12.     transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(aimRotation), Time.deltaTime * aimSpeed);
    13. }
     
  17. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    It sort of worked! The thing is, for some reason it rotates the player to the y rotation of 0.8790234. I looked at my camera and nothing matches the number. Here are my camera's numbers so maybe someone could explain this to me.

    Camera:

    Position:
    X-1094.603
    Y-1.633766
    Z-1011.637

    Rotation:
    X-0
    Y-234.1831
    Z-0

    Scale:
    X-1
    Y-1
    Z-1

    This is in edit mode of course.
     
    Last edited: Feb 28, 2015
  18. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    Sorry I haven't been able to test until now which is why I've made so many mistakes. Just tested this out and it works perfectly:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. @SerializeField
    4. var aimSpeed : float;
    5.  
    6. function Update () {
    7.     if (Input.GetKey(KeyCode.Mouse1))
    8.         Aim();
    9. }
    10. function Aim() {
    11.     var aimRotation : Vector3 = transform.rotation.eulerAngles;
    12.     aimRotation.y = Camera.main.transform.rotation.eulerAngles.y;
    13.     transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(aimRotation), Time.deltaTime * aimSpeed);
    14. }
    Try that out and let me know if it works for you too.
     
  19. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Oh my gosh, thank you so much!!! It works perfectly!!! I'm sorry I had so much trouble, but you we're still a lot of help. A lot of people on here are jerks. Again, thanks!!! I just Have one more question. How would I make it work with the Horizontal and Vertical axis. I'm going to use it instead of my current rotation script.
     
    Last edited: Mar 1, 2015
  20. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    This should work:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. @SerializeField
    4. var aimSpeed : float;
    5.  
    6. function Update () {
    7.    if (Input.GetKey(KeyCode.Mouse1))
    8.         Aim();
    9. }
    10. function Aim() {
    11.     var aimRotation : Vector3 = Camera.main.transform.rotation.eulerAngles;
    12.     aimRotation.z = transform.rotation.eulerAngles.z;
    13.     transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(aimRotation), Time.deltaTime * aimSpeed);
    14. }
     
    Last edited: Mar 1, 2015
  21. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    Just edited my post (I read your new question wrong).
     
  22. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I was looking more to do the same thing as the Aim script but only when you move. Here's what I tried
    Code (CSharp):
    1. #pragma strict
    2. @SerializeField
    3. var aimSpeed : float;
    4. function Update () {
    5.     if (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))
    6.         Aim();
    7. }
    8. function Aim() {
    9.     var aimRotation : Vector3 = transform.rotation.eulerAngles;
    10.     aimRotation.y = Camera.main.transform.rotation.eulerAngles.y;
    11.     transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(aimRotation), Time.deltaTime * aimSpeed);
    12. }
     
  23. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    I don't think I fully understand what you're trying to do but you can try this:

    Code (JavaScript):
    1. #pragma strict
    2. @SerializeField
    3. private var aimSpeed : float;
    4. function Update() {
    5.     Aim();
    6. }
    7. function Aim() {
    8.     var xRotation : float = Input.GetAxis("Vertical");
    9.     var yRotation : float = Input.GetAxis("Horizontal");
    10.     transform.Rotate(xRotation, yRotation, 0, Time.deltaTime * aimSpeed);
    11. }
     
  24. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Sorry, I know it's been a while. I have another question (no surprise). I want to reverse the order of this script. By that I mean instead of the player changing the camera's rotation, I want a certain empty gameobject to change the camera's rotation. I'm really sorry again for asking so many questions but I'm not great at coding. Thanks for the help!
     
  25. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    By the way I figured out the movement thing I asked about before.