• 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
Question by mrlafle · Nov 30, 2015 at 12:28 AM · javascript3rd person controller3rd person camera

Help With Camera-Dependent 3rd Person Control

Hello everyone, I'm working on my first 3D platformer in Unity and I could use some help with my custom 3rd person controller. And sorry BTW if this has been asked and answered already!

So here's my situation. I have a 3rd person control system where I can rotate the camera around the hero (with the right stick) without making him turn as well. When I tilt the left stick upwards, the hero should turn and walk AWAY from the camera, no matter what direction it's pointed. But I cannot figure out how to program this. In my program the hero walks in the same global direction, even if it doesn't match up with the view. Here is my java script.

 #pragma strict
 
 //declaring object variables
 var controller:GameObject;        //gameObject with ControllerScript attatched
 var hero:GameObject;            //"Player" gameObject -- moves hero AND camera when running
 var heroMesh:GameObject;        //"Mesh" gameObject -- moves hero (and not camera) when jumping
 var cam:GameObject;            //"CamRotY" gameObject. This is the camera's parent that rotates                                           on the y-axis
 
 //declaring speed & state variables
 internal var runSpd:float = 0.5;
 internal var facing:Vector3;
 internal var grounded:boolean;
 
 //declaring controller variables
 internal var moveY:float;        //move hero forward/back
 internal var moveX:float;        //move hero left/right
 internal var jump:boolean;        //make hero jump

 function Update () 
 {
     print(moveY);
 
     //defining controller variables
     moveY = controller.GetComponent(ControllerScript).leftStickY;
     moveX = controller.GetComponent(ControllerScript).leftStickX;
     jump = controller.GetComponent(ControllerScript).butA;
     
     //Determine hero facing with left stick 
     if(moveX || moveY){
         facing = new Vector3(moveX, 0, -moveY);
         heroMesh.transform.forward = facing;
     }
     
     
     //Move player in forward direction
     hero.transform.position.z += -moveY * runSpd;
     hero.transform.position.x += moveX * runSpd;

}

Sorry if my code is a little redundant, I'm a little new with Javascript

And thanks for the help!!!

Comment

People who like this

0 Show 0
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

38 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

Related Questions

Transform.InverseTransformDirection ALMOST worked with character-facing direction!!!! 1 Answer

How to fix "the type or namespace ".." could not be found" in unity 0 Answers

3rd Person Movement / Camera Control Help or Advice Needed 1 Answer

3rd Person Camera Controller, Character should follow the Camera Movement 0 Answers

Need help with my 3RD person Script being buggy!! 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