- Home /
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;
}
}
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
Answer by AurimasBlazulionis · Dec 29, 2014 at 12:14 PM
Try:
YourCamera.transform.forward;
Or:
YourCamera.Vector3.forward;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Rotate Turret to Tag 1 Answer
Rotate Locally Player Towards Joystick/Axis Input 1 Answer
How to open and close a door parented to a rotating object? 0 Answers