Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by sherlockturtle · Apr 29, 2012 at 09:49 PM · characterrotatemovment

Make Camera and Character Face Same Direction

So i have this code(i know its a lot). [code]

var character : Transform; var distance : float = 5.0; //How far away to orbit var xSpeed : float = 50.0; //X sensitivity var ySpeed : float = 50.0; //Y sensitivity

var resetTime : float = 1.0; //How long to take to reset

private var x : float = 0.0; //Y rotation private var y : float = 0.0; //X rotation

private var rotation : Quaternion; //Current orientation

private var resetting : boolean = false; //resetting? private var resetTimer : float = 0.0; //How long we've been resetting

var speed = 8;//8run 4 walk var rotatespeed = 4; var walkonoff : boolean = false; var cameraDistanceMax: float = 10; var cameraDistanceMin: float = 5; var scrollSpeed: float = 1; @script AddComponentMenu("Camera-Control/Mouse Orbit and reset") //Add to menu

function LateUpdate() { //Every frame, do this as late as you can

 if (character) {//There's a character
     if(!resetting && Input.GetMouseButtonUp(0)) { //Released mouse button
          resetting = true; 
          endX = x;
          endY = y;
          resetTimer = 0.0; //Reset the reset timer
     }

     if(!resetting) {
         if (Input.GetMouseButtonDown(0)) { //Pushed mouse button
             //Initialize the angles
             var angles = transform.eulerAngles;
             x = angles.y;
             startX = x;
             y = angles.x;
             startY = y;
         } //first time the button down

         if(Input.GetMouseButton(0)) { //Mouse button is down
             //Change the angles by the mouse movement
             x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
             Reorient();
         } //button held down
     } //Camera moving
     else { //Reset
         resetTimer += Time.deltaTime; //add to the timer;
         var amountReset = resetTimer / resetTime; //How far we are

         Reorient();
         if(resetTimer >= resetTime) resetting = false;
     } //Reset
     character.forward = transform.forward;//make camera and character face forward
 } //There's a character

} //LateUpdate

function Reorient() { //orient yourself //Rotate the camera to those angles rotation = Quaternion.Euler(y, x, 0); transform.rotation = rotation;

 //Move the camera to look at the character
 transform.position = rotation * Vector3(0.0, 0.0, -distance)
                               + character.position;
                               

}

function Update(){

//zoom

distance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed; cameraDistance = Mathf.Clamp(distance, cameraDistanceMin, cameraDistanceMax);

 // set camera position



//movment controls

    //wasd
     if (Input.GetKey ("w"))
      character.Translate(Vector3.forward * speed * Time.deltaTime);

Translate(Vector3.forward speed Time.deltaTime); if (Input.GetKey ("s")) character.Translate(Vector3.back speed Time.deltaTime); Translate(Vector3.back speed Time.deltaTime); if (Input.GetKey ("q")) character.Translate(Vector3.left speed Time.deltaTime); Translate(Vector3.left speed Time.deltaTime); if (Input.GetKey ("e")) character.Translate(Vector3.right speed Time.deltaTime); Translate(Vector3.forward speed Time.deltaTime); if (Input.GetKey ("a")) character.Rotate(0,-1 *rotatespeed,0); character.Rotate(0,-1 *rotatespeed,0); if (Input.GetKey ("d")) character.Rotate(0,1*rotatespeed,0); character.Rotate(0,1 *rotatespeed,0);

if(Input.GetKeyDown("/"))

if(walkonoff == true) walkonoff = false; else if (walkonoff == false) walkonoff = true;

if(walkonoff == true){ speed=4; rotatespeed = 2; animation.play("Walk"); }

if(walkonoff == false){ speed=8;rotatespeed = 4; animation.play("Run"); }
if (Input.GetKey("/")){

}

}

[/code] But the problem is if i rotate the camera with the mouse the character does not move wiht it. How could i fix that.

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 HHameline · Apr 29, 2012 at 10:36 PM 0
Share

Is this script on you Camera? Or your Character?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by UniteMage · Apr 29, 2012 at 10:49 PM

Hello

There is an excellent tutorial on the third person character controller at 3DBuzz (http://www.3dbuzz.com/).

In the following method of the tutorial, I added a single line as indicated below:

 Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
 {
     Vector3 direction = new Vector3(0, 0, -distance);
     
     // Added line
     rotationY = Character.transform.eulerAngles.y + rotationY;
     
     Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
     return TargetLookAt.position + rotation * direction;
 }

Where ever the characters faces, the camera looks in the same direction....

I recommend studying the tutorial at 3DBuzz....

I hope this helps.

CSDG

Comment
Add comment · Show 1 · 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
avatar image sherlockturtle · Apr 30, 2012 at 12:07 AM 0
Share

So how would i implements just that into my script? Because i dont use rotation Y.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change gravity direction on Character Motor 0 Answers

Character Collision Issue 1 Answer

Bug with my enemyAI 2 Answers

Gyroscope problem (nexus S, Android: 4.0) 1 Answer

My character jumps occationally when against a wall? 2 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