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 z25assassins · Apr 25, 2016 at 04:32 AM · directiontranform.position

How to move the Rigid body with respect to the camera angle

Not trying to beat a dead horse here because I know this question has been asked so many times. But I'm still having trouble trying to figure this out. I'm working on modifying the first tutorial from unity. I got my camera to move horizontally left and right, But when I go forward even though I am facing to either the left or right, When I press "up" I still am moving along the Z axis. So what I think I need to do(correct me if I'm wrong) is to change the Z axis with respect to the camera's current angle. This in turn will allow me to move forward in the direction of the camera?alt text

This is what I have tried to do "code wise". This is the Player class.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;

 public class PlayerController : MonoBehaviour {
 public float speed;
 public Text countText;
 public Text winText; 
 private int count;

 public Vector3 movement;
 public CameraController c;
 private Rigidbody rb;
 public Transform target;
 public Vector3 targetDir;
 // Use this for initialization
 void Start () {
 
     rb = GetComponent<Rigidbody> ();
     count = 0;
     winText.text = ""; 
 }
 
 // Update is called once per frame
 //called before rendering a frame

 void Update () {
     transform.forward = new Vector3 (rb.position.x + c.directionView, 0, 0);
     transform.right = new Vector3 (0, 0, rb.position.y + c.directionView);
     print (transform.forward);
 }
 //called just before calling any physics calculations
 void FixedUpdate () {
     
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");
     movement = new Vector3 (moveHorizontal,0.0f,moveVertical);
     rb.AddForce (movement * speed);
     //rb.AddRelativeForce (movement*speed);
 }

 void OnTriggerEnter(Collider other){
     if(other.gameObject.CompareTag("Pick up")){
         
         other.gameObject.SetActive(false);
         count += 1;
         setCountText ();
     }
 }

 void setCountText(){
     countText.text = "Count : " + count.ToString ();

     if (count >= 8) {
         winText.text = "You Win!";
     }
 }
     

}

This is the Camera class.

 using UnityEngine;
 using System.Collections;

 public class CameraController : MonoBehaviour {
 PlayerController pc;
 public GameObject player;
 public float directionView;//camera direction left and right


 // Use this for initialization
 void Start () {
 }
 
 // Update is called once per frame
 void LateUpdate () {
     
     transform.position = player.transform.position;
     //transform.eulerAngles = new Vector3 (50, 0, 0);

     if (Input.GetKey ("right")){
         turnRight ();
     }
     else if (Input.GetKey ("left")) {
         turnLeft ();
     }
     else if (Input.GetKey ("up")) {
         transform.rotation = Quaternion.Euler (0, directionView, 0);
     }
     else if (Input.GetKey ("down")) {
         transform.rotation = Quaternion.Euler (0, directionView, 0);
     }
     print (directionView);
 }

 void turnRight(){
     directionView += 20;
     transform.rotation = Quaternion.Euler (0, directionView, 0);
     if (directionView == 360) {
         directionView = 0;
     }
 }
 void turnLeft(){
     directionView -= 20;
     transform.rotation = Quaternion.Euler (0, directionView, 0);
     if (directionView == -360) {
         directionView = 0;
     }
 }
     

}

screenshot-18.png (471.9 kB)
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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Moving Randomly 1 Answer

"Fake" Vector3.forward? 3 Answers

How to rotate an object to face the direction it's going? 1 Answer

jump to the direction of the getaxis 1 Answer

Stop player following cameras cameras direction 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