Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by benjimazza · Dec 07, 2011 at 10:58 PM · animationcollider

How to jump on to a turret gun

Okay so this is a MASSIVE question and would help me a number amount in my game :).... To go in to detail,,, i have modelled a turret gun, and When i approach the This turret gun i want it so what basicly when i hit a box collider that acts as a trigger or somthing, i can press like "X" to jump on to the turret gun Then hit "X" again to jump off of the turret gun,,, it anybody could help me then

Thanks

Comment
Add comment · Show 2
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
avatar image syclamoth · Dec 07, 2011 at 10:59 PM 3
Share

This isn't detailed at all.

avatar image KyleHickman · Dec 08, 2011 at 12:56 AM 2
Share

Nor a question...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Dec 08, 2011 at 04:01 AM

I suppose this is the classic "first person enters the vehicle" case: you move around as a first person character, but when approaching the vehicle you may "enter" and drive it - or control the turret, in your case.
You should child an empty object to the turret - let's call it "PlayerPos" - to indicate the position and rotation of the player when on the turret. When you press the "jump to turret" key, a special function disable the player movement script, move/rotate the player to "PlayerPos", child it to "PlayerPos" and enable the turret control script.
To jump off the turret, you must have another empty object childed to the turret to define the "out of turret" position - let's call it "PlayerExit". Another function disable the turret control script, move/rotate the player to "PlayerExit", unchild it and enable the player movement script.
To ensure this will happen only when the player is next to the turret, a trigger is used: when the player enters the trigger, the variable player is set to the player transform and canJump enables the "jump on turret" key.
Child a rectangular trigger to the turret, and attach this script to the trigger object:

private var playerPos: Transform; private var playerExit: Transform; private var player: Transform; private var onTurret = false; private var canJump = false; private var jumping = false; // signals when the player is jumping

function JumpOn(duration: float){ // jump on the turret jumping = true; // signals that the player is jumping // disable MovePlayerScript (replace with actual name) player.GetComponent.<MovePlayerScript>().enabled = false; var startPos = player.position; var startRot = player.rotation; var t: float = 0; while (t < 1){ // move the player to playerPos t += Time.deltaTime/duration; player.position = Vector3.Lerp(startPos, playerPos.position, t);
player.rotation = Quaternion.Slerp(startRot, playerPos.rotation, t);
yield; } player.parent = playerPos; // child player to playerPos onTurret = true; // tell it's on the turret // enable TurretControl script (replace with the actual name) transform.parent.GetComponent.<TurretControl>().enabled = true; jumping = false; // jump finished }

function JumpOff(duration: float){ jumping = true; // warns that the player is jumping // disable TurretControl script (replace with the actual name) transform.parent.GetComponent.<TurretControl>().enabled = false; var center = player.position - Vector3.up; // set the Slerp center var startPos = player.position - center; // start and end pos are var endPos = playerExit.position - center; // relative to center var startRot = player.rotation; var t: float = 0; while (t < 1){ t += Time.deltaTime/duration; // add center to make the point relative to the world player.position = Vector3.Slerp(startPos, endPos, t) + center;
player.rotation = Quaternion.Slerp(startRot, playerExit.rotation, t);
yield; } player.parent = null; // unchild the player onTurret = false; // player is no more on the turret // enable MovePlayerScript (replace with the actual name) player.GetComponent.<MovePlayerScript>().enabled = true; jumping = false; // player ended jumping player = null; // disable jump }

// on trigger enter identifies the player and enables jump on function OnTriggerEnter(col: Collider){ if (col.tag == "Player"){ player = col.transform; // set the player variable and enable jump canJump = true; } }

function OnTriggerExit(col: Collider){ if (col.tag == "Player"){ canJump = false; // no jump if outside trigger } }

function Start(){ transform.parent.GetComponent.<TurretControl>().enabled = false; playerPos = transform.parent.Find("PlayerPos"); playerExit = transform.parent.Find("PlayerExit"); }

function Update(){ if (!jumping){ // do nothing if already jumping if (onTurret){ // if on turret, jumps off if (Input.GetKeyDown("x")){ JumpOff(0.6); } } else { // if off turret, jumps on if (canJump && Input.GetKeyDown("x")){ JumpOn(0.8); } } } } In a brief, you must child to the turret: a box trigger, an empty object called "PlayerPos" and an empty object called "PlayerExit". Attach the script above to the trigger object, and adjust "PlayerPos" to the position and direction the player must be when in the turret, then adjust "PlayerExit" to the position and orientation the player will "land" when jumping off. Finally, name the player movement and turret control scripts as "MovePlayerScript.js" and "TurretControl.js".

Comment
Add comment · Show 7 · Share
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
avatar image benjimazza · Dec 08, 2011 at 04:11 PM 0
Share

i got a bunch of errors from this code :/

avatar image aldonaletto · Dec 09, 2011 at 11:54 AM 0
Share

This code was not tested and may contain several errors, but the basic idea is ok. Which errors did you get? Remember that $$anonymous$$ovePlayerScript and TurretControl are fake names for your player movement and turret control scripts, and must be replaced by the actual names - not to mention that they must be scripts dedicated to their basic functions, so that disabling one of them doesn't affect other functions: if the health control is part of $$anonymous$$ovePlayerScript, for instance, the player may become invulnerable while in the turret.

avatar image benjimazza · Dec 10, 2011 at 06:43 PM 0
Share

i have tryed fixing the code, its not worked :/

avatar image FLASHDENMARK · Dec 10, 2011 at 08:20 PM 1
Share

It is not necessarily meant to "work", it is meant for you to understand. The code provided contains the general idea the OP is trying to achive, errors or not; it is irrelevant. Aldonaletto provided this "concept-code"(so to speak) so you can build on top of that.

avatar image aldonaletto · Dec 12, 2011 at 03:55 AM 1
Share

@benjimazza, I finally tested and fixed the errors in the code. I also added a Start function with the necessary initialization, and the whole thing worked very fine. Try the version above - and pay attention to the turret game object structure.

Show more comments
avatar image
0

Answer by KyleHickman · Dec 08, 2011 at 01:27 AM

Semi Pseudo Scripting Answer:

A. If you are thinking of a way to judge distance by using the collider i would recommend creeating a script that checks the difference of the Turret and the collider. so: var rangeOfGettingInTurret : float; var player : GameObject;

private var distance : float;

function update() { distance = Vector3.Distance(transform.position,player.transform.position);

if(distance <= rangeOfGettingInTurret) { /Do What needs to be done about checking input and such/ }

}

Just to clarify that was something i just thought of and probably wont work like that...

B.Here are some things you should check out.

Input.GetButton()

Vector3.Distance()

I hope i could help or spark something! :D

Comment
Add comment · Share
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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collider Animation 1 Answer

Can I make animations snap to a frame? 1 Answer

OnTriggerStay2D Breaks When Adding AnimationController 1 Answer

Help with making an animation play for X amount of seconds? 1 Answer

How to stop an animation on collision 0 Answers


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