• 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 /
avatar image
0
Question by blanklearn · May 24, 2017 at 07:41 AM · cameramovementtransformdirection

Need help camera relative movement, script works but if camera is on z values need to be positive, if camera is on x values need to be negative

Hey guys I'm a pretty amateur coder and I've been looking for a simple camera relative movement controller and this is as close as I've come so far, it keeps the camera position relative to the player while looking at a pivot that is set to the players position.

my problem is when i have the cam on the world z axis (behind or in front of character) positive values work for relative movement but if i use positive values with my camera on the world x axis the player moves in the opposite direction to the input relative to the camera.

the problem appears to be in the last two lines of code.

if someone could explain to me what i could do to fix this and why its happening it would help me greatly.

using UnityEngine; using System.Collections;

public class RelativeMovement : MonoBehaviour {

 //camComponents
 Camera cam;
 Transform pivot;
 Vector3 offset;

 //playerComponents
 Animator anim;
 GameObject player;
 Rigidbody rb;
 float walkSpeed = 4.0f;

 //inputComponents
 public float horizontal;
 public float vertical;

 void Start () {
     anim = GetComponent<Animator> ();
     cam = Camera.main;
     player = GameObject.FindGameObjectWithTag ("Player");
     rb = GetComponent<Rigidbody> ();
     pivot = cam.transform.parent.transform;
     offset = transform.position - cam.transform.position;
 }
     
 void Update () {
     InputMoveFindCameraPos ();
     if (Input.GetKeyDown("e")){    anim.Play ("RightHook");    }
 }

 void LateUpdate(){
     cam.transform.position = transform.position - offset;
     cam.transform.LookAt (pivot);
 }

 void InputMoveFindCameraPos(){
     
     horizontal = Input.GetAxis ("Horizontal");
     vertical = Input.GetAxis ("Vertical");
     Vector3 moveDir = new Vector3 (horizontal, 0, vertical);

     if (moveDir != Vector3.zero) {
         //Move pivot with player and position it up for a better view of character
         pivot.position = transform.position + Vector3.up;

         //find the direction the camera is looking in and use that with input
         Vector3 relativeToCam = cam.transform.InverseTransformDirection (moveDir);
         // set the y difference to 0 so player doesn't move away from the ground
         relativeToCam.y = 0.0f;

         // this is where the problem is 
         // these next two lines work but using them 
         // as positive works: when camera is on z axis
         // as negative works: when camera is on x axis 
         transform.rotation = Quaternion.LookRotation (-relativeToCam); 
         rb.MovePosition (transform.position - (relativeToCam * walkSpeed * Time.deltaTime));
     }
 }

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Movement relative to Camera and XZ Plane 1 Answer

Disable Camera Movement,Stop camera movement 0 Answers

Moving player in direciton camera is facing 1 Answer

Jerky Camera and Varying Translate speeds 1 Answer

How to smoothly rotate camera around an object with touch 1 Answer


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