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 /
avatar image
0
Question by Obeon · Aug 25, 2017 at 04:07 PM · movementcharactermovement script

Make character move according to camera position.

Hi. I'm new to unity (really new) and i'm having a problem trying to make the character move according to the camera view.

I used the transform.Translate to make the character move according to the global coordinates. but when I move the camera, i have that problem when I move the camera in which the forward direction is the backwards for the camera view. I wanted that it changed according to what I'm seeing on the camera. I searched everywhere, but the solutions I found either didn't work, or I didn't know how to adapt into the code I'm using (beucase of the inexperience, maybe)

The code I'm using on the character is:

public class PlayerController : MonoBehaviour {

  private Rigidbody rb;
  public float speed = 10.0F;
  public float jump = 3.0F;
  public float run = 20.0F;
  Animator anim;
  void Start ()
  {
      rb = GetComponent<Rigidbody> ();
      anim = GetComponent<Animator> ();
  }
          
  void Update()
  {
      float moveHorizontal = Input.GetAxis ("Horizontal") ;
      float moveVertical = Input.GetAxis ("Vertical");
                          
      Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
      if (movement != Vector3.zero) 
      {
          transform.Translate (movement * speed * Time.deltaTime, Space.World);
          anim.SetBool ("walk", true);
          transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(movement), 0.2f);
      }
      else
      {
          anim.SetBool ("walk", false);
      }
      if (movement != Vector3.zero && Input.GetButton("Fire2"))
      {            
          transform.Translate (movement * run * Time.deltaTime, Space.World);
          anim.SetBool ("fastRun", true);
          transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(movement), 0.2f);
      }
      else
          anim.SetBool ("fastRun", false);
      if (Input.GetButtonDown("Fire1")) 
      {
          if (anim.GetBool ("walk") == false)
              anim.SetTrigger ("Jump");
          else 
          {
              rb.AddForce (0.0f, jump, 0.0f, ForceMode.Impulse);
              anim.SetTrigger ("Jump");
          }
      }
  
  }

}

To move the camera i'm using:

public class CameraControl : MonoBehaviour {

  public GameObject Player;
  public float rotationSpeed = 4.0f;
  private Vector3 offset;
  void Start ()
  {
      offset = transform.position - Player.transform.position;
  }
  void LateUpdate ()
  {
      offset = Quaternion.AngleAxis (Input.GetAxis ("Mouse X") * rotationSpeed, Vector3.up) * offset;
      offset = Quaternion.AngleAxis (Input.GetAxis ("Mouse Y") * rotationSpeed, Vector3.right) * offset;
      transform.position = Player.transform.position + offset;
      transform.LookAt (Player.transform.position);
          
  }

}

Can anyone help me? (I'm also having a problem trying to set a limit to the quaternion.angleaxis, so the camera doesn't go through the ground, but one problem at a time).

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

Answer by Vicarian · Aug 25, 2017 at 04:23 PM

In general you'll parent the camera to the player's transform. If it's a first person game, you'll parent the camera somewhere in or near the character's head. To solve the issue with rotation going into the ground, look at Mathf.Clamp.

Comment
Add comment · Show 5 · 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 Obeon · Aug 25, 2017 at 05:20 PM 0
Share

It's third person. Parenting the camera made it turn around when the player turms, but I only want the camera to turn around with the analog stick while accompaining the player (for that I used the second script).

The problem I'm having with the player movement is that if the camera is facing the wolrd forward, I can press up to go forward relative to the camera. But if it is facing the world backwards and I press up, the player go backwards relative to the camera, because his transform is oriented based on the world. I wanted it to always go forward relative to the camera when i press up, for example.

For the camera through the ground, should I use mathf.clamp with the transform.position? Can you give me an example?

avatar image Vicarian Obeon · Aug 25, 2017 at 05:29 PM 0
Share

I don't see a point where you're rotating the player in the playerController script. As far as mathf.clamp:

 float m_rx = 0;
 
 void Update() {
         float      my, ry;
         Quaternion rotation    = Quaternion.identity;
         
         ry    = Input.GetAxis("$$anonymous$$ouse X");
         my    = Input.GetAxis("$$anonymous$$ouse Y");
         m_rx += my * m_speed;
 
         // calculate horizontal rotation based on raw mouse input data
         rotation    = Quaternion.Euler(new Vector3(0, ry, 0));
 
         // Apply horizontal rotation to player
         transform.rotation *= rotation;
 
         // Clamp rotation about the x axis to 180 degrees in front of the player (first - person) from mouse data
         m_rx = $$anonymous$$athf.Clamp(m_rx, -90, 90);
 
         // calculate vertical rotation
         rotation = Quaternion.Euler(new Vector3(-m_rx, 0, 0));
 
         // Update the camera parented to the player with the new vertical rotation
         GetComponentInChildren<Camera>().transform.localEulerAngles = 
             new Vector3(-m_rx, 
                         GetComponentInChildren<Camera>().transform.localEulerAngles.y, 
                         GetComponentInChildren<Camera>().transform.localEulerAngles.z);
     }
avatar image Obeon Vicarian · Aug 25, 2017 at 08:21 PM 0
Share

I'm rotating on line 22 and 32, with this code:

transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(movement), 0.2f);

the problem is i'm moving with these codes:

float moveHorizontal = Input.GetAxis ("Horizontal") ; float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); transform.Translate (movement speed Time.deltaTime, Space.World);

then, when the camera is facing the back of the world, the character doesn't change the way it moves, so if I press up in the analog stick, he goes forward.

about the code you put, that is assu$$anonymous$$g the camera is a child of the player? I must put it as a child before using the code?

Show more comments

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

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

Character on Moving and Rotating Platform 0 Answers

Rigidbody Player Controller sticking to walls 0 Answers

character(ball) rolls with less speed when I build it 2 Answers

I have a movement script, but how can i make it so i can move half the speed while in the air. 0 Answers

Move Character to touched Position 2D (Without RigidBody and Animated Movement) 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