Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DBBakerdesign · Dec 08, 2015 at 11:56 AM · c#javascriptphysicscolliders

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Drag and Drop Objects in Game Problem 2 Answers

Reset a collider during runtime to lose previous Physics.IgnoreCollision() calls 1 Answer

Is there a way to make child colliders not be classed as parent colliding? 1 Answer

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges