The question is answered, right answer was accepted
How to move forward (towards z), ignoring any rotations?
Hello. I have an object that moves forward along its z axis when you press w, but the object will move up or down if either x or y have any values. I want to ignore these axis and move only forward.
example, you are walking forward with your head tilted down. your body moves forward, not down towards where you are looking.
What im trying to do is make a basic arcadey style helicopter controls. When you push forward, the helicopter moves forward, but to make it looks a bit more "real" the helicopter tilts forward about 15 degrees. When this occurs, the helicopter then travels forward and down. I want to just have the helicopter move forward, not down as well.
(im using Unity5.5.0f3)
heres my code (editing out the irrelevant parts):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ApacheScript : MonoBehaviour{
public Rigidbody helicopterRigidbody;
private float leftRightRot;
private float frontBackRot;
private float leftRightMin;
private float leftRightMax;
private float frontBackMin;
private float frontBackMax;
private Quaternion zRotation;//leftRight
private Quaternion xRotation;//frontBack
private Vector3 temp;
private float enginePower = 1000f;
public bool isOnGround = true;
void Start (){
leftRightRot = 0f;
leftRightMax = 30f;
leftRightMin = -30f;
frontBackMin = -15f;
frontBackMax = 15f;
frontBackRot = 0;
helicopterRigidbody = gameObject.GetComponent<Rigidbody> ();
}
void Update () {
temp = transform.eulerAngles;
if (Input.GetKey (KeyCode.W)) {
helicopterRigidbody.drag = 1;
helicopterRigidbody.useGravity = false;
transform.Rotate (2, 0, 0);
helicopterRigidbody.AddForce(transform.forward * (70f * enginePower));
}
if (Input.GetKey (KeyCode.S)) {
helicopterRigidbody.drag = 1;
helicopterRigidbody.useGravity = false;
transform.Rotate (-2, 0, 0);
helicopterRigidbody.AddForce(transform.forward * (-40f * enginePower));
}
if (temp.z > 31f && temp.z <= 180f) {
temp.z = 30f;
transform.eulerAngles = temp;
} else if (temp.z > 180f && temp.z < 329f) {
temp.z = 330f;
transform.eulerAngles = temp;
}
if (temp.x > 16f && temp.x <= 180f) {
temp.x = 15;
transform.eulerAngles = temp;
} else if (temp.x > 180f && temp.x < 344) {
temp.x = 345;
transform.eulerAngles = temp;
}
}
}
Answer by Pengocat · Feb 08, 2017 at 03:43 AM
Should work if you use Vector3.forward
instead of transform.forward
. the transform forward vector is the blue axis relative to world space.
This works until the helicopter turns (rotates), then "forward" isn't forward relative to the way the helicopter is facing.
Answer by Joeltron · Feb 08, 2017 at 03:07 PM
I made it work by using the local transform.forward, and then compensating for the downward motion by adding a slight amount of force pushing the rigidbody up. Took a bit of tweaking, but works fine.
Follow this Question
Related Questions
Fixedjoints Not Updating Position In UNet 0 Answers
My Enemies walk through objects and my FPS character model 3 Answers
calculate what angle should the turret fire with a rigibody bullet [C#] 0 Answers
Question about NavMesh and Rigidbody 0 Answers
Issues Getting my GameObject to both Move Forward and Jump properly. 0 Answers