Question by
roy47 · Mar 12, 2017 at 10:49 AM ·
rigidbodypositionrigidbody.velocity
How can i move a rigidbody according to its local axis?
The character always moves North, not the direction its looking at. the rigidbody moves to space's axis not the rigidbody's axis Heres the full script:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 6f;
public float JumpVelocity = 10f;
public GameObject cameraObject;
public Rigidbody rb;
Vector3 movement;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate ()
{
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent<MouseLook>().currentYRotation, 0);
//rb.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
}
void Move (float h, float v)
{
/*/
if (Input.GetAxisRaw("Horizontal") != 0)
{
rb.AddRelativeForce(h * speed * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
else
{
rb.velocity = Vector3.zero;
}
if (Input.GetAxisRaw("Vertical") != 0)
{
rb.AddRelativeForce(0, 0, v * speed * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetButtonDown("Jump"))
{
rb.AddForce(0, JumpVelocity * Time.deltaTime, 0, ForceMode.VelocityChange);
}
/*/
movement.Set(h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
rb.position = (transform.position + movement);
}
}
Comment
Your answer
Follow this Question
Related Questions
how to change the velocity of a rigid body based on a forward 0 Answers
How could I enact two forces on a rigidbody? 0 Answers
Why is new Vector3 stronger when player is in the air? 0 Answers
Limit object y position 0 Answers
Moving rigidbody with addforce, velocity or position causes another object not to collide anymore. 0 Answers