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 Besbes_Salma · Jan 04, 2017 at 10:59 AM · localglobal3rd person controller3rd person camera

My character moves according to global Vectors

This seems like it should be something simple, i'm moving my character by changing his velocity with a camera locked behind my player, which i can rotate it around and snap it back behind him whenever he change his direction.

The problem is when i move my player, for exemple, to the right and snap the camera behind him to look at his forward vector then try to move forward, my character will move toward the global forward Vector (in this exemple you'll see your character going to the left)

Anyone have any ideas?


Here's My HeroControll Script:


 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))]
 [RequireComponent(typeof(Animator))]
 [RequireComponent(typeof(CapsuleCollider))]
 
 public class HeroControll : MonoBehaviour {
 
     public float PlayerSpeed,jumpHeight;
     public bool grounded,moving;
     public GameObject Camera;
 
     private Rigidbody rg;
     private Animator anim;
     private float forwardInput, turnInput;
     private bool runInput;
     private int r;
     private RaycastHit hit;
     
 
 
     // Use this for initialization
     void Start () {
         rg = GetComponent<Rigidbody>();
         anim = GetComponent<Animator>();
         forwardInput = turnInput = 0;
         jumpHeight = 10f;
         PlayerSpeed = 10f;
         grounded = true;
         moving = false;
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         //Get Inputs
         forwardInput = Input.GetAxis("Vertical");
         turnInput = Input.GetAxis("Horizontal");
         runInput = Input.GetKey(KeyCode.LeftShift);
       
 
         //Set Animations
         if ((turnInput != 0) || (forwardInput != 0))
         {
             moving = true;
             anim.SetBool("walking", true);
         }
         else
         {
             moving = false;
             anim.SetBool("walking", false);
         }
 
         //Update velocity 
         rg.velocity = new Vector3(PlayerSpeed * turnInput, rg.velocity.y, PlayerSpeed * forwardInput);
 
         //Set Directions
 
         moveDirection = new Vector3(turnInput, 0, forwardInput);
         if (moveDirection != Vector3.zero)
         {
             Quaternion newRotation = Quaternion.LookRotation(moveDirection);
             transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 8);
             
         }
     }
 }


and My CameraControll Script :

 using UnityEngine;
 using System.Collections;
 
 public class CameraZelda : MonoBehaviour {
 
     private GameObject player;
     private float smooth,orb;
     public Transform target;
 
     private Vector3 offset;
 
 
     // Use this for initialization
     void Start () {
         player = GameObject.FindGameObjectWithTag("Player");
         smooth = 30f;
         offset = player.transform.position - transform.position;
     }
     
     // Update is called once per frame
     void Update () {
         transform.position = player.transform.position - offset;
         
 
         orb = Input.GetAxis("orb");
         transform.RotateAround(player.transform.localPosition, Vector3.up, smooth * orb * Time.deltaTime);
 
         changeTarget();
         
         
     }
 
     void changeTarget()
     {
         if (Input.GetKey(KeyCode.C))
         {   
             transform.position = Vector3.Slerp(transform.position, target.position, smooth * Time.deltaTime);
             offset = player.transform.position - transform.position;
             Quaternion rotation = Quaternion.LookRotation(player.transform.forward);
             transform.rotation = rotation;
             transform.eulerAngles = new Vector3(20f, transform.eulerAngles.y, transform.eulerAngles.z);
 
         }
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by KGC · Jan 05, 2017 at 03:09 PM

The solution is to convert your movement vectors from global to local (relative to the camera). Found another question for this with a solution. Link: http://answers.unity3d.com/questions/8444/moving-player-relative-to-camera.html

Commented version of the code, and some examples of how to integrate with your code (not tested):

 // Get local camera forward axis aligned with floor plane
 // Convert global forward to localspace forward for camera's transform
 Vector3 forward = Camera.transform.TransformDirection(Vector3.forward);
 
 // Get rid of the Y value to align it with floor plane (could also project the vector, but this is easier to understand)
 forward.y = 0f;
 
 // Ensure it is a direction (normalized) and not a position
 forward = forward.normalized;
 
 // Calculate relative right vector for localspace forward vector from camera
 Vector3 right = new Vector3(forward.z, 0.0f, -forward.x);
 
 // Update velocity 
 // Add forward and turn vectors together (which are both scaled to speed and input)
 rg.velocity = (forward * PlayerSpeed * turnInput) + (right * PlayerSpeed * forwardInput);
 
 //Set Directions
 // Why not just sample from the rigidbody's velocity?
 moveDirection = rg.velocity.normalized;
Comment
Add comment · Show 3 · 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 Besbes_Salma · Jan 05, 2017 at 07:18 PM 0
Share

$$anonymous$$mmm, Okey i see what u'r talking about, i'll try to follow this logic. thx :D

avatar image KGC Besbes_Salma · Jan 05, 2017 at 07:27 PM 0
Share

Alternatively, you could use rg.transform.forward ins$$anonymous$$d of trying to calculate forward using the camera. It gives a different feel to the controls - for it to feel like it based on the camera, you'd need to move the camera near-instantaneously to achieve the same effect (around the Y axis).

avatar image Besbes_Salma · Jan 06, 2017 at 12:21 PM 1
Share

It Worked Perfectly :D thank you so much

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

88 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 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

How to convert a direction Vector3 from world to local space 1 Answer

Need help with a 3rd person Camera 0 Answers

How to make a space ship fly after crosshair? 1 Answer

3rd person movement with camera between platforms with gravity 0 Answers

How can i change from local to global axis in a rolling ball movment,How can i change from local to global axis. 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