Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by coreygunter92 · Mar 03, 2016 at 06:16 PM · scripting problemcamera movement

Camera Controller Script - locked Y Axis

Hello all, I'm relatively new to Unity and I've been working on writing my own character and camera controller scripts. I've watched a few different videos and I've been able to cobble together some code that does essentially what I want, aside from the fact that I would like the Y axis to be controlled by mouse movement while also being horizontally locked to the game object. The following is my CharacterController Script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CharacterController : MonoBehaviour {
 
     [System.Serializable]
     public class MoveSettings
     {
         public float forwardVel = 12;
         public float leftVel = 8;
         public float rightVel = 8;
         public float strafeVel = 12;
         public float rotateVel = 100;
         public float jumpVel = 5;
         public float distToGrounded = 0.6f;
         public float controlDownAcceleration = 2;
         public LayerMask ground;
     }
     [System.Serializable]
     public class PhysSettings
     {
         public float downAccel =  .5f;
         public float controlDownAccel = 5f;
     }
     [System.Serializable]
     public class InputSettings
     {
         public float inputDelay = 0.1f;
         public string FORWARD_AXIS = "Vertical";
         public string TURN_AXIS = "Horizontal";
         public string JUMP_AXIS = "Jump";
         public string MOUSE_AXIS_X = "Mouse X";
         public string MOUSE_AXIS_Y = "Mouse Y";
         public string FALL_AXIS = "Downwards";
         
     }
 
     public MoveSettings moveSetting = new MoveSettings();
     public PhysSettings physSetting = new PhysSettings();
     public InputSettings inputSetting = new InputSettings();
 
     
 
     Vector3 velocity = Vector3.zero;
     public Quaternion targetRotation;
     
     Rigidbody rBody;
     float forwardInput, turnInput, jumpInput, mouseInputX, mouseInputY, downwardsInput;
 
     public Quaternion TargetRotation
     {
         get { return targetRotation; }
     }
 
    
     
     bool Grounded()
     {
         
         return Physics.Raycast(transform.position, Vector3.down, moveSetting.distToGrounded, moveSetting.ground);
         
     }
 
     void Start()
     {
         targetRotation = transform.rotation;
         
         if (GetComponent<Rigidbody>())
             rBody = GetComponent<Rigidbody>();
         else
             Debug.LogError("This character needs a rigidbody.");
         
         forwardInput = turnInput = jumpInput = 0;
 
     }
 
     void GetInput()
     {
         forwardInput = Input.GetAxis(inputSetting.FORWARD_AXIS);
         turnInput = Input.GetAxisRaw(inputSetting.TURN_AXIS); //get axis interpolates -1 to beyond
         mouseInputX = Input.GetAxis(inputSetting.MOUSE_AXIS_X);
         mouseInputY = Input.GetAxis(inputSetting.MOUSE_AXIS_Y);
         downwardsInput = Input.GetAxis(inputSetting.FALL_AXIS);
         jumpInput = Input.GetAxisRaw(inputSetting.JUMP_AXIS); // not interpolated (return 1, -1, 0)
 
 
     }
 
     void Update()
     {
         GetInput();
         Turn();
         
         
         
         
         
     }
 
     void FixedUpdate()
     {
         Run();
         Jump();
         Strafe();
 
         rBody.velocity = transform.TransformDirection(velocity);
         
     }
 
     void Run()
     {
         if (Mathf.Abs(forwardInput) > inputSetting.inputDelay)
         {
             //move
             velocity.z = moveSetting.forwardVel * forwardInput;
             
         }
         
         else
         {
             velocity.z = 0;   
         }
     }
 
     void Turn()
     {
         
         if (Input.GetAxis("Mouse X") > 0)
         {
             targetRotation *= Quaternion.AngleAxis(moveSetting.rotateVel * mouseInputX * Time.deltaTime, Vector3.up); 
         }
         else if (Input.GetAxis("Mouse X") < 0)
         {
             targetRotation *= Quaternion.AngleAxis(moveSetting.rotateVel * mouseInputX * Time.deltaTime, Vector3.up);
         }
            
         transform.rotation = targetRotation;
     }
 
     
 
    
 
     void Jump()
     {
         if (jumpInput > 0 && Grounded() )
         {
             //jump
             velocity.y = moveSetting.jumpVel;
             
         }
         else if (jumpInput == 0 && Grounded())
         {
             //zero out velocity
             velocity.y = 0;
             
         }
         else if (jumpInput > 0 && !Grounded())
         {
             velocity.y++;
         }
         else if (downwardsInput > 0 && !Grounded())
         {
             Debug.Log("You have pressed shift");
             velocity.y--;
         }
         else
         {
             //decrease velocity.y
             velocity.y -= physSetting.downAccel;
         }
     }
 
     void Strafe()
     {
         if (Input.GetKey(KeyCode.A))
         {
             
             velocity.x = moveSetting.strafeVel * -1;
             
         }
         else if (Input.GetKey(KeyCode.D))
         {
             
             velocity.x = moveSetting.strafeVel;
         }
 
         else
         {
             velocity.x = 0;
         }
         
         
     }
 }
 

The CharacterController script is working as intended I just wanted to include the code just in case. The next chunk is my FollowTarget script that is the standard asset and I just changed it for my uses. What I want the camera to do is update the Y axis with Mouse Y input while it also is facing the same direction as the GameObject, the player, it is nested inside of. Currently it just locks the Y axis and transform.forward matches target.forward. using System; using UnityEngine;

 namespace UnityStandardAssets.Utility
 {
     public class FollowTarget : MonoBehaviour
     {
         private Camera camera;
         public Transform target;
         public Vector3 yAxisVector;
         public float yAxisValue;
         public Vector3 offset = new Vector3(0f, 7.5f, 0f);
         public float smooth = 2.0f;
 
         void Update()
         {
             if (Input.GetAxis("Mouse Y") > 0)
             {
                 Debug.Log("Greater than zero!");
 
             }
         }
 
         private void LateUpdate()
         {
             transform.position = target.position + offset; // set camera position relative to playerobject
             transform.forward = target.forward; // makes camera face the playerobject's direction but locks Y axis
             
             
 
            
         }
     }
 }
 

I've narrowed the problem down to the transform.forward = target.forward line but I don't know of a way around the issue that I'm facing. Any help would be appreciated!! Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

50 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D CameraController Script 0 Answers

A different approach to flashlight movement. 0 Answers

Vuforia: occlude image target? 1 Answer

Setting Velocities through Input 1 Answer

No input coming from using new input system, and variable not viewed in inspector. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges