How can i simulate entering/exit a spaceship logic ?
The spaceship is not object that the player can walk inside or enter it and i don't want to control the spaceship. All i want is when the user touch the spaceship to give the player some realism of entering and sitting in the spaceship. The i will make that the spaceship will move on it's own and that the user will be able to rotate the camera around 360 degrees looking around.
The problem is how to make something realism to simulate entering and exit the spaceship.
In my logic when the player touch the spaceship the first person player should rotate smooth facing to the spaceship front direction.
This script is attached to the first person controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnterExitSpaceship : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
var _direction = (other.transform.position - transform.position).normalized;
var rot = Quaternion.LookRotation(Vector3.up, _direction);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * 1);
print("Im in !");
}
private void OnTriggerExit(Collider other)
{
print("Im out !");
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
And also this control script to control the first person character is attached to it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterController : MonoBehaviour
{
public float speed = 10.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
float translatioin = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translatioin *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translatioin);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
The first person controller also have a Rigidbody and capsule collider components. And as child the Main Camera.
The spaceship have a Box Collider component and Is Trigger is checked.
This is a screenshot of the spaceship just a second before i touch it and should simulate the entering.
The problem now is when i touch the spaceship it's getting to the OnTriggerEnter event but then the first person character is rotating fast and in many direction a big mess.
And i'm not sure that my entering logic to the spaceship is right. Not sure how to do it.
Your answer
Follow this Question
Related Questions
I create material with script but it does not render right 0 Answers
Creating Splines from empties in script 0 Answers
How can i rotate all the child objects together at the same time ? 1 Answer
How can i give another name/number to the created Plane object name ? 0 Answers
How can i create List of maps from each Map class ? 0 Answers