• 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 Zedock · Dec 29, 2014 at 11:44 AM · rotationlocal

Using RigidBody, how can I make the character move in the direction the camera is facing

I think it may have something to do with Script1 * Script2, however I am not sure.

My two scripts are:

using UnityEngine; using System.Collections;

public class MainPlayer : MonoBehaviour {

 public float speed = 500;
 public float speedAc = 10;
 private Vector3 zeroAc;
 private Vector3 curAc;
 private float sensH = 10;
 private float sensV = 10;
 private float smooth = 0.5f;
 private float GetAxisH = 0;
 private float GetAxisV = 0;

 public GUIText SpeedText;
 
 // Use this for initialization
 void Start () {
     ResetAxes();

     SpeedText.text = speed.ToString();
 }


 void FixedUpdate () {
     
     if (SystemInfo.deviceType == DeviceType.Desktop) {
         float movehorizontal = Input.GetAxis ("Horizontal");
         float movevertical = Input.GetAxis ("Vertical");
         
         Vector3 movement = new Vector3 (movehorizontal, 0.0f, movevertical);
         rigidbody.AddForce (movement * speed * Time.deltaTime);
     }
     else 
     {
         curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
         GetAxisV = Mathf.Clamp(curAc.y * sensV, -1, 1);
         GetAxisH = Mathf.Clamp(curAc.x * sensH, -1, 1);
         
         Vector3 movement = new Vector3 (GetAxisH, 0.0f, GetAxisV);
         
         rigidbody.AddForce(movement * speedAc);
         
     }
 }

 void ResetAxes(){
     zeroAc = Input.acceleration;
     curAc = Vector3.zero;
 }

}

using UnityEngine; using System.Collections;

public class MainCamera : MonoBehaviour {

 public Transform target;
 public float distance = 10.0f;
 public float sensitivity = 3.0f;
 
 private Vector3 offset; 
 
 void Start ()  {
     offset = (transform.position - target.position).normalized * distance;
     transform.position = target.position + offset;
 }
 
 void Update () {
     Quaternion q = Quaternion.AngleAxis(Input.GetAxis ("CameraRight") * sensitivity, Vector3.up);
     offset = q * offset;
     transform.rotation = q * transform.rotation;
     transform.position = target.position + offset;
 }

}

Comment
Add comment · Show 2
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 Zedock · Dec 29, 2014 at 08:45 AM 0
Share

Where would this go? tanoshimi

avatar image Zedock · Dec 29, 2014 at 08:57 AM 0
Share

I did I will show what my current scripts are,

Using UnityEngine; using System.Collections;

public class $$anonymous$$ainPlayer : $$anonymous$$onoBehaviour {

 public float speed = 500;
 public float speedAc = 10;
 private Vector3 zeroAc;
 private Vector3 curAc;
 private float sensH = 10;
 private float sensV = 10;
 private float smooth = 0.5f;
 private float GetAxisH = 0;
 private float GetAxisV = 0;

 public GUIText SpeedText;
 
 // Use this for initialization
 void Start () {
     ResetAxes();

     SpeedText.text = speed.ToString();
 }


 void FixedUpdate () {
     
     if (SystemInfo.deviceType == DeviceType.Desktop) {
         float movehorizontal = Input.GetAxis ("Horizontal");
         float movevertical = Input.GetAxis ("Vertical");
         
         Vector3 movement = new Vector3 (movehorizontal, 0.0f, movevertical);
         rigidbody.AddForce (movement * speed * Time.deltaTime);
     }
     else 
     {
         curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
         GetAxisV = $$anonymous$$athf.Clamp(curAc.y * sensV, -1, 1);
         GetAxisH = $$anonymous$$athf.Clamp(curAc.x * sensH, -1, 1);
         
         Vector3 movement = new Vector3 (GetAxisH, 0.0f, GetAxisV);

         rigidbody.AddForce(camera.main.transform.forward * speedAc);
         
     }
 }

 void ResetAxes(){
     zeroAc = Input.acceleration;
     curAc = Vector3.zero;
 }
 

}

The only thing I changed was the addforce part, and the scripts are assigned to two completely different objects, the first "Player" being attached to the player, and the "Camera script" Being attached to a camera which follows the player.

The error I am getting is:

Assets/Original/Chaper 1/Scripts/$$anonymous$$ainPlayer.cs(43,51): error CS0176: Static member `UnityEngine.Camera.main' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d

tanoshimi

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AurimasBlazulionis · Dec 29, 2014 at 12:14 PM

Try:

YourCamera.transform.forward;

Or:

YourCamera.Vector3.forward;

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Rotate Turret to Tag 1 Answer

Min, Max Rotation Help 2 Answers

Creating Loading Bar For web Player 1 Answer

Instantiate then access component(s) 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