- Home /
How do I switch from Character player to Airship Vehicle using triggers?
Hello Unity, first time on these forums, and I'm hoping I can get some quick answers as I am working with a deadline of.... tomorrow. :)
My Question here is: How do I attach a script to both my vehicle, and my Character, so that, at once, with the press of a single key (Input.GetKey("r")), I can enable and disable many different components and scripts on one or the other prefab so that I may switch from the player character, to the vehicle character.
Let me break it down. I am making a 3D Steampunk game that I wanted to be as simplistic, mechanically, as possible so that it could be a good, but still ambitious, first team project. The terrain assets are beautiful, the character is all animated, and the airship vehicle looks very nice and it has an interior.
We know how to 3D model and animate, but we don't have a scriptwriter/programmer. Any code I reference or seem to understand on these forums is what I've learned this past week. I've been breaking my back learning to animate my character through Mecanim, then script the walking controls, and I also made a custom script for the airship that doesn't just translate forward and to the side, but rotates and goes up and down.
So basically, I don't know how to program at all, and I'm only picking up scraps of information here and there, mostly just duct taping random scripts together with a vague idea of what I just did.
How my game project is set up right now is, I have a character prefab "Character Prefab" with the character model, and character camera called "Charcam" parented underneath it.
Components of Character Prefab are as follows: -animator -rigidbody -capsule collider -Basic Walk Script which has
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Rigidbody))]
public class basicWalkScript : MonoBehaviour
{
[System.NonSerialized]
public float moveSpeed = 4.0f;
public float turnSpeed = 50f;
[System.NonSerialized]
public float animSpeed = 1.5f;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void OnAnimatorMove()
{
if (anim)
{
if (Input.GetKey("w"))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if (Input.GetKey("s"))
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
if (Input.GetKey("a"))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
if (Input.GetKey("d"))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
anim.SetFloat("speed", v);
anim.SetFloat("direction", h);
anim.speed = animSpeed;
if (v <= 0.0f)
{
moveSpeed = 4;
}
else
{
moveSpeed = 6;
}
}
}
My Airship vehicle prefab "AShipPrefab", has the airship model, and "Airship cam" parented underneath it.
Components of airship prefab are -Rigidbody -Sphere Collider (solid, not trigger) -Testfly Script including var walkSpeed: float = 70.0; var turnSpeed = 200; var elevateSpeed = 50.0; var strafeSpeed = 20; var backSpeed = 10;
function Start () {
}
function Update () {
if(Input.GetKey("w"))
{
transform.Translate(Vector3(0,0,1) * Time.deltaTime * walkSpeed);
}
if(Input.GetKey("s"))
{
transform.Translate(Vector3(0,0,-1) * Time.deltaTime * backSpeed);
}
if(Input.GetKey("q"))
{
transform.Translate(Vector3(-1,0,0) * Time.deltaTime * strafeSpeed);
}
if(Input.GetKey("e"))
{
transform.Translate(Vector3(1,0,0) * Time.deltaTime * strafeSpeed);
}
if (Input.GetKey("d"))
{
transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime);//turns right
}
if (Input.GetKey("a"))
{
transform.Rotate(-Vector3.up * turnSpeed * Time.deltaTime);//turns left
}
if (Input.GetKey("left shift"))
{
transform.Translate(Vector3(0,-1,0) * Time.deltaTime * elevateSpeed);
}
if (Input.GetKey("space"))
{
transform.Translate(Vector3(0,1,0) * Time.deltaTime * elevateSpeed);
}
}
So that's about it. I was having uber difficulty trying to figure this out and really need help, I also know there is a much easier way to do this than using triggers even so if you could tell me how to do that, that would be great. The code can be in either c# or javascript, preferably jscript just so i could possibly understand what is going on.