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 /
This question was closed Aug 07, 2016 at 05:16 AM by AgonyRoses for the following reason:

Solved it myself

avatar image
0
Question by AgonyRoses · Aug 06, 2016 at 07:54 PM · movementcharactercontrollercharacter controllercharacter movementthird person controller

Character Controller Move in X and Z axis via camera

Hello so I'm trying to move my thirdperson character controller relative to the camera's direction... It works, but however it moves on it's own and also moves on the Y-Axis if looking upwards or downwards. How can I fix this so that it doesn't move up or down?

TP_Motor Script for Movement

 using UnityEngine;
 using System.Collections;
 
 public class TP_Motor : MonoBehaviour
 {
     public static TP_Motor Instance;
     public Vector3 camForward;
     public float MoveSpeed = 10f;
 
     public Vector3 MoveVector { get; set; }
 
     void Awake()
     {
         Instance = this;
     }
 
     public void UpdateMotor() //not update since the function needs to be called on manually
     { //when UpdateMotor is called, call these functions as well
 
         //SnapAlign();
         ProcessMotion();
         RotateMotion();
     }
 
     void ProcessMotion() // to move the direction of the model's direction i.e strafing
     {
 
         // Transform MoveVector to Worldspace
         MoveVector = transform.TransformDirection(MoveVector);
         // Normalize MoveVector if Magnitude > 1
         if (MoveVector.magnitude > 1)
             MoveVector = Vector3.Normalize(MoveVector);
         // Multiply MoveVector by MoveSpeed
         MoveVector *= MoveSpeed;
         // Multiply MoveVector by DeltaTime
         MoveVector *= Time.deltaTime;
         // Move in World Space 
         TP_Controller.CharacterController.Move(MoveVector);
         //MoveVector = Camera.main.transform.TransformDirection(MoveVector);
     }
 
     void SnapAlign() //a function used to snap the character forwards.. i don't use this for the time being
     {
         if (MoveVector.x != 0 || MoveVector.z != 0)
         {
             transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
                 Camera.main.transform.eulerAngles.y,
                     transform.eulerAngles.z);
         }
     }
     
     void RotateMotion() //This is to move direction relative to the camera
     {
         // Transform MoveVector to Worldspace
         MoveVector = Camera.main.transform.TransformDirection(Vector3.forward);
         // Normalize MoveVector if Magnitude > 1
         if (MoveVector.magnitude > 1)
             MoveVector = Vector3.Normalize(MoveVector);
         // Multiply MoveVector by MoveSpeed
         MoveVector *= MoveSpeed;
         // Multiply MoveVector by DeltaTime
         MoveVector *= Time.deltaTime;
         // Move in World Space 
         TP_Controller.CharacterController.Move(MoveVector);
         //MoveVector = Camera.main.transform.TransformDirection(MoveVector);
     }
 }

ThirdPerson_Controller Script for handling inputs

 using UnityEngine;
 using System.Collections;
 
 public class TP_Controller : MonoBehaviour {
 
     //note that a static variable is an unchanging variable that isn't manipulated.
     public static CharacterController CharacterController;
     public static TP_Controller Instance;
 
     void Awake()
     {
         CharacterController = GetComponent("CharacterController") as CharacterController;
         Instance = this;
         TP_Camera.ExistingOrNewCamera();
 
     }
 
     void Update()
     {
         if (Camera.main == null)
             return;
 
         GetLocomotionInput();
 
         TP_Motor.Instance.UpdateMotor();
     }
 
     void GetLocomotionInput()
     {
         var deadZone = 0.1f;
 
         TP_Motor.Instance.MoveVector = Vector3.zero;
 
         if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
             TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
 
         if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
             TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
     }
 }
 

TP_Camera Script for the Camera using UnityEngine; using System.Collections;

 public class TP_Camera : MonoBehaviour
 {
 
     public static TP_Camera Instance;
 
     public Transform TargetLookAt;
     public float Distance = 15f;
     public float DistanceMin = 10f;
     public float DistanceMax = 20f;
     public float DistanceSmooth = 0.05f;
     
     public float X_MouseSensitivity = 5f;
     public float Y_MouseSensitivity = 5f;
     public float WheelSensitivity = 15f;
     public float X_Smooth = 0.05f;
     public float Y_Smooth = 0.01f;
     public float Y_MinLimit = -40f;
     public float Y_MaxLimit = 80f;
 
     private float mouseX = 0f;
     private float mouseY = 0f;
     private float velX = 0f;
     private float velY = 0f;
     private float velZ = 0f;
     private float velDistance = 0f;
     private float startDistance = 0f;
     private float desiredDistance = 0f;
     private Vector3 position = Vector3.zero;
     private Vector3 desiredPosition = Vector3.zero;
 
     void Awake()
     {
         Instance = this;
     }
 
     void Start ()
     {
         Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax); //Set Distance's distance minimum and max distance
         startDistance = Distance;
         Reset();
     }
     
 
     void LateUpdate ()
     {
        if (TargetLookAt == null)
             return;
 
         HandlePlayerInput();
 
         DesiredPosition();
 
         UpdatePosition();
     } 
 
 
     void HandlePlayerInput()
     {
         var deadZone = 0.01f;
         //if(Input.GetMouseButton(1))
         {
             // RMB is down get mouse Axis input
             mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
             mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
         }
 
         // Limit mouseY
         mouseY = Helper.ClampAngle(mouseY, Y_MinLimit, Y_MaxLimit); //send the helper clamp angle into our mouseY
 
         if(Input.GetAxis("Mouse ScrollWheel") < -deadZone 
             || Input.GetAxis("Mouse ScrollWheel") > deadZone)
         {
             desiredDistance = Mathf.Clamp(Distance - Input.GetAxis("Mouse ScrollWheel") *
                 WheelSensitivity, DistanceMin, DistanceMax);
         }
     }
 
     void DesiredPosition()
     {
         //Evaluate distance
         Distance = Mathf.SmoothDamp(Distance, desiredDistance, 
             ref velDistance, DistanceSmooth);
         //Calculate desired position
         desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
     }
 
     Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
     {
         Vector3 direction = new Vector3(0, 0, -distance);
         Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
         return TargetLookAt.position + rotation * direction;
     }
 
     void UpdatePosition()
     {
         var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, X_Smooth);
         var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, Y_Smooth);
         var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, X_Smooth);
         position = new Vector3(posX, posY, posZ);
 
         transform.position = position;
 
         transform.LookAt(TargetLookAt);
 
     }
 
     public void Reset()
     {
         mouseX = 0;
         mouseY = 10;
         Distance = startDistance;
         desiredDistance = Distance;
     }
 
     public static void ExistingOrNewCamera() //Checking for a main camera.. 
         //If one isn't there it makes a new one
     {
         GameObject tempCamera;
         GameObject targetLookAt;
         TP_Camera myCamera;
 
         if (Camera.main != null)
         {
             tempCamera = Camera.main.gameObject;
         }
         else
         {
             tempCamera = new GameObject("Main Camera");
             tempCamera.AddComponent<Camera>();
             tempCamera.tag = "MainCamera";
         }
         tempCamera.AddComponent<TP_Camera>();
         myCamera = tempCamera.GetComponent<TP_Camera>() as TP_Camera;
 
         targetLookAt = GameObject.Find("targetLookAt") as GameObject;
 
         if(targetLookAt == null)
         {
             targetLookAt = new GameObject("targetLookAt");
             targetLookAt.transform.position = Vector3.zero;
         }
 
         myCamera.TargetLookAt = targetLookAt.transform;
     }
 }
 
Comment
Add comment · Show 1
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
avatar image AgonyRoses · Aug 05, 2016 at 09:18 PM 0
Share

Worth noting the Rotate$$anonymous$$otion() is the one i need help with.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by AgonyRoses · Aug 07, 2016 at 10:07 AM

Never mind solved it myself. I had to add some extra variables:

 public Vector3 camForward;
 public Transform cam;

and change RotateMotion() into this:

void RotateMotion() //This is to move direction relative to the camera {

 cam = Camera.main.transform;         
 float h = Input.GetAxis("Horizontal");
 float v = Input.GetAxis("Vertical");

 camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
 MoveVector = (v * camForward + h * cam.right).normalized;
 MoveVector *= Time.deltaTime;
 MoveVector *= MoveSpeed;
 TP_Controller.CharacterController.Move(MoveVector);

}

So now I have a function for moving relative to the camera and the other for moving relative to the direction faced.

Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers

Player acts weirdly when I release movement input 0 Answers

How to make character to face in specific direction.,How To Rotate Character When Arrow Key Is Pressed? 0 Answers

Character is running through terrain instead of over it 0 Answers

Make the CharacterController stop adjusting for slopes 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