- Home /
how do I make a obeject (player) move the way it's pointing ? (like a jetpack) (c#)
Hi , have been looking how to fix this for some time now and haven't found how to so if you know how to fix it and write that , thank you.
I am working on a game where you fly arond with a jetpack (in almost no gravity) and can rotate the character .The script I made works but not as I want it to. When I press space it the player/obeject moves up but it always go up. When I rotate the character it still moves the same way (when I press space it moves up , even if my character is upside down) and I haven't found a way how to fix that.
script I am using (c#) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float JetSpeed;
public float RotateSpeed = 30f;
void Update() {
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(-Vector3.forward * -30 * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.forward * -30 * Time.deltaTime);
}
**// this is the part that makes the player fly/jump**
if (Input.GetKeyDown(KeyCode.Space))
{
transform.position = transform.up * JetSpeed;
}
}
}
Thanks for reading and have a great day! // HAPPY_SIR
Answer by voncarp · Jan 23, 2017 at 08:29 AM
I readjusted your rotation and forward movement functionality as they didnt seem to move in a meaningful way. Something like this might be what you are looking for :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class helpscript : MonoBehaviour {
public float JetSpeed;
public float RotateSpeed = 30f;
void Update() {
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(-Vector3.down * -30 * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.down * -30 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Space))
{
transform.position += transform.forward * JetSpeed * Time.deltaTime;
}
}
}
You might have to adjust the rotations as I don't know if you are using 2D or 3D
You may want to use Space.self/LocalPosition ins$$anonymous$$d of using the world coordinate so when the object is rotated you move upwards in the direction of the object and not upwards relative to the world.
[Range(1f, 50f)]
public float JetSpeed = 10f;
[Range(1f, 100f)]
public float RotateSpeed = 30f;
void Update()
{
bool rotateLeft = Input.Get$$anonymous$$ey($$anonymous$$eyCode.A);
bool rotateRight = Input.Get$$anonymous$$ey($$anonymous$$eyCode.D);
bool flyUpwards = Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space);
if (rotateLeft && rotateRight)
{
// No rotation
}
else if (rotateLeft)
{
transform.Rotate(Vector3.back * RotateSpeed * Time.deltaTime);
}
else if (rotateRight)
{
transform.Rotate(Vector3.forward * RotateSpeed * Time.deltaTime);
}
// this is the part that makes the player fly/jump**
if (flyUpwards)
{
var translateSpeed = JetSpeed * Time.deltaTime;
transform.Translate(Vector3.up * translateSpeed, Space.Self);
}
}
Thanks so much for the answares , but sadly I am using 2d , and what I am trying to do is make the character fly in the derection it is pointing. But Thanks for the answare anyways! I am really thankfull for your answer!