Reputation: 3782
Currently I have got a spaceship which flies around a planet. It looks like this
and I use this code
public class PlayerMovement : MonoBehaviour
{
private float currentMovementSpeed = 30;
private float rotationSpeed = 80;
private Rigidbody rigid;
private void Start()
{
rigid = GetComponent<Rigidbody>();
}
private void Update()
{
// move the spaceship
rigid.MovePosition(rigid.position + transform.TransformDirection(new Vector3(0, 0, 1)) * currentMovementSpeed * Time.deltaTime);
// rotate the shaceship by pressing A or D
transform.Rotate(0, Input.GetAxis(StringCollection.INPUT_HORIZONTAL) * rotationSpeed * Time.deltaTime, 0);
}
}
So when I press A I want to tip the ship to the left
and when pressing D I want to tip ship to the right
Does someone know how to do that?
Upvotes: 1
Views: 5494
Reputation: 51
You could also check out this asset - all the code is commented and it's super smooth - helped me a lot.
Basically, it has a pretty versatile script to do LERP based cam following of the ship. This is kind of like a normal Smooth Follow, except you get it in full 3D, so the ship can go fully upside down, etc. The controls are easily changeable, which I did myself, but the default is that the ship is continually moving and you use arrow keys for pitch/yaw.
Here's the main idea - you have a childed camera target object and the camera uses LERPing to follow it - gives a super interesting effect for access/deceleration:
// lerp the camera back to its correct position
_cam.transform.localPosition = Vector3.Lerp(_cam.transform.localPosition, _camTarget.transform.localPosition, 1f - CameraLag);
_cam.transform.localRotation = Quaternion.Lerp(_cam.transform.localRotation, _camTarget.transform.localRotation, 1f - CameraLag);
Upvotes: 0
Reputation: 7605
Instead of this:
transform.Rotate(0, Input.GetAxis(StringCollection.INPUT_HORIZONTAL) * rotationSpeed * Time.deltaTime, 0);
You should use something like this:
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
Here is a sample where it is used:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
You may be interested in watching the following tutorial, since it is similar to what you are trying to create:
https://unity3d.com/es/earn/tutorials/projects/space-shooter/moving-the-player?playlist=17147
Upvotes: 3