Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Have character's upper body follow mouse vertical movement.

Discussion in 'Physics' started by SuperGCNBoy, Mar 13, 2018.

  1. SuperGCNBoy

    SuperGCNBoy

    Joined:
    Dec 14, 2013
    Posts:
    11
    Hello,

    I am attempting to create a simple third-person shooter. I am having trouble getting my character's upper half to follow the vertical movement of my mouse.

    I am able to have the upper half respond to my input but it makes the upper half twist and rotate in unexpected ways.

    I noticed that the orientation of the spine on my character is rotated on two axes compared to the model as a whole.

    If someone could point me in the right direction that would be appreciated.

    Thanks,
    Kent

    This is my CameraController script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour {
    6.     public float rotationHorizontalSpeed = 2.0F;
    7.     public float rotationVerticalSpeed = 2.0F;
    8.     public GameObject upperBody;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.      
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.         float h = rotationHorizontalSpeed * Input.GetAxis("Mouse X");
    18.         float v = rotationVerticalSpeed * Input.GetAxis("Mouse Y");
    19.         transform.Rotate(v, h, 0);
    20.     }
    21.  
    22.     private void LateUpdate()
    23.     {
    24.         //this.upperBody.transform.up = this.transform.forward;
    25.         //this.upperBody.transform.forward = this.transform.right;
    26.         //this.upperBody.transform.right = -this.transform.up;
    27.     }
    28. }
    Images of my workspace:
    https://i.imgur.com/u1yJGFC
    https://imgur.com/x7dOkmt

    Character model:
    https://assetstore.unity.com/packages/3d/characters/humanoids/toon-soldiers-ww2-demo-85702
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    I think it's because you are rotating the transform relative to itself every frame. That means your rotations are affected by the previous rotations.

    I'm not sure how you want your game to behave so I can't tell you how to go about correcting the problem.

    You could try splitting your rotations onto separate gameobjects and only rotated x on one and y on the other.

    Or

    You could rotate relative to the parent transform

    There are plenty of other solutions that may or may not be right for you.
     
  3. OOVUM

    OOVUM

    Joined:
    Mar 8, 2020
    Posts:
    17
    Antony can you help me convert this to c sharp. Its the only script i can find that will move your spine with mouse movement but unity doesn't support c sharp anymore. Iv been stuck on this for hours. Help would be very much appreciated.

    1. var sensitivityZ = 15F;
    2. var minimumZ = -60F;
    3. var maximumZ = 60F;
    4. var rotationZ = 0F;
    5. function LateUpdate ()
    6. {
    7. rotationZ += Input.GetAxis("Mouse Y") * sensitivityZ;
    8. rotationZ = Mathf.Clamp (rotationZ, minimumZ, maximumZ);
    9. transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y,rotationZ);
    10. }
    11. function Start ()
    12. {
    13. // Make the rigid body not change rotation
    14. if (rigidbody)
    15. rigidbody.freezeRotation = true;
    16. }
     
  4. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    replace 'var' with float

    replace 'function' with void

    put everything inside a class. (which is created for you if you create a new script from unity)
     
    OOVUM likes this.
  5. OOVUM

    OOVUM

    Joined:
    Mar 8, 2020
    Posts:
    17
    Thank you for replying Antony. But i already tried that and tried it again but it wont work. something about API's keeps popping up and I've tried to fix it in many ways. If its fine with you could you create a script that would allow my back to bend with up and down camera movement. Im creating a third person shooter and need it for my shooting script. I want to apply the script to my back bone segments.For Example:

    https://www.crazygames.com/game/warscrapio

    see how the players back rotates when you move the camera up and down but their is a limit to how far you can extend. That is what i am looking for. I understand this is a lot to ask for but i am new to unity and coding so help would be appreciated.
     
  6. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    It's probably better you learn yourself. If you get others to write your code without the understanding then you won't be able to fix problems of add to the code later on. You will just run into more and more issues as you continue to develop your game.
     
  7. OOVUM

    OOVUM

    Joined:
    Mar 8, 2020
    Posts:
    17
    I understand, I'll just keep on trying. By the way i tried out Major Mayhem and I love your work! thanks for replying.
     
    Antony-Blackett likes this.
  8. OOVUM

    OOVUM

    Joined:
    Mar 8, 2020
    Posts:
    17
    Sorry for asking so many questions, but i made this script and i'm having some errors. As you know, I'm trying to make my character look up and down with the camera. I applied this script to the Spine but it makes my body rotate in weird ways. It still looks up and down but my spine is twisted sideways. I've scanned the forums and it probably has something to do with my spine rotation. If you could point me in the right direction it would be appreciated.

    public Transform Spine;
    void LateUpdate()
    {
    Spine.rotation = Quaternion.LookRotation(Camera.main.transform.forward, Spine.up);
    Spine.transform.Rotate(0, 0, 30);
    }
     
  9. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    you're probably rotating around the wrong axis.

    Spine.transform.Rotate(30, 0, 0);

    If that doesn't fix it then I don't know enough about your game to help you.