I’m trying to follow a tutorial on a flying rocket game (where you fly around objects and try not to hit them.
The player (A pig) rotates only to its left and right and tilts over. I need it to rotate on the axis that will allow it to rotate foward and back so when the player presses the “Space” bar the character will hover in the right direction. The image entails how the player(pig) falls to its side not rotating the in the right axis.
ps: The “A” and “D” Keys are used/pressed for rotating.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/* Vector3 direction = otherObject.position - transform.position;
Quaternion toRotation = Quaternion.FromToRotation(transform.up, direction);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.time);*/
public class takeFlight : MonoBehaviour
{
AudioSource audiosource;
public Rigidbody rigidBody;
float speed = 100f;
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audiosource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
processInput();
}
private void processInput()
{
if ((Input.GetKey("a")))
{
transform.Rotate(Vector3.forward * speed * Time.deltaTime);
}
else if ((Input.GetKey("d")))
{
transform.Rotate(-Vector3.forward * speed * Time.deltaTime);
}
else if (Input.GetKey("space"))
{
rigidBody.AddRelativeForce(Vector3.up * speed * Time.deltaTime);
}
else
{
}
}
}