Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Narshyl · Mar 27, 2012 at 11:25 AM · playerlaunchcannon

Launching player with cannon

Hello guys, i've just started with unity and i'm stuck. I'm trying to make a cannon that will launch my player to the next part of my map.

i can't find anything on how to do this. please help.

(i've googled it but nothing showed up)

Comment
Add comment · Show 1
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 Kryptos · Mar 27, 2012 at 02:13 PM 0
Share

Did you try to write a piece of code yourself?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Mar 27, 2012 at 02:20 PM

Make an animation, parent character to the animated root (and disable character movement etc) for the duration of the animation.

If you want something physics driven, you might want to add a rigid body to the object, disable any character movement scripts, call rigidbody.AddForce with the direction you want to fire it and use force mode impulse. Upon collision you might want to disable the rigidbody and reactivate the character again.

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
avatar image
0

Answer by FlavioVolpeJr · Oct 16, 2020 at 08:25 PM

I spent about 3 days trying to make this script, now that I got it, it has worked well in my project. Here he goes to help anyone looking for the same thing. Remembering that it is for 3D games.

 using System;
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class PlayerControlCannon : MonoBehaviour
 {
     public Rigidbody character;
     public Transform shootingPart;
     public bool manualControl;
     public float rotatingSpeed = 100f;
     public float shotForce = 50f;
 
     private bool isShooting = false;
     private float backColliderDelay = 0.05f;
 
 
     public void Start()
     {
 
     }
 
 
     public void Update()
     {
 
         if (Input.GetButtonDown("Fire1") && isShooting == true)
         {
 
             StartCoroutine("Shooting");
 
             print("Fired");
 
         }
     }
 
     IEnumerator Shooting()
     {
 
         character.transform.position = transform.position;
         character.transform.rotation = shootingPart.rotation;
 
         character.AddForce(transform.forward * shotForce, ForceMode.VelocityChange);
 
         character.transform.parent = GameObject.FindWithTag("Player").transform;
         character.GetComponent<MeshRenderer>().enabled = true;
         character.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
         character.GetComponent<Collider>().enabled = false;
 
         yield return new WaitForSeconds(backColliderDelay);
 
         character.GetComponent<Collider>().enabled = true;
 
         isShooting = false;
 
     }
 
     public void OnTriggerStay(Collider other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
 
             other.gameObject.transform.parent = transform;
             other.gameObject.transform.localPosition = new Vector3(0f, 0f, 0f);
             other.gameObject.transform.localRotation = Quaternion.identity;
             other.gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
             other.gameObject.GetComponent<MeshRenderer>().enabled = false;
 
             isShooting = true;
 
         }
 
         if (manualControl == true)
 
         {
             // Move to the right
             if (Input.GetButton("Horizontal") && Input.GetAxisRaw("Horizontal") > 0)
             {
                 transform.Rotate(Vector3.up * rotatingSpeed * Time.deltaTime);
             }
 
             // Move to the left
             else if (Input.GetButton("Horizontal") && Input.GetAxisRaw("Horizontal") < 0)
             {
                 transform.Rotate(Vector3.down * rotatingSpeed * Time.deltaTime);
             }
 
             // Move to the up
             else if (Input.GetButton("Vertical") && Input.GetAxisRaw("Vertical") < 0)
             {
                 transform.Rotate(Vector3.right * rotatingSpeed * Time.deltaTime);
             }
 
             // Move to the down
             else if (Input.GetButton("Vertical") && Input.GetAxisRaw("Vertical") > 0)
             {
                 transform.Rotate(Vector3.left * rotatingSpeed * Time.deltaTime);
             }     
         }
     }
 }

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

7 People are following this question.

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

Related Questions

How Can I Make A Platform Lauch My PLayer Into The Air? 2 Answers

Launching my character with a cannon. 3 Answers

AI/Player gun firing issue 0 Answers

errors when launching unity player... 1 Answer

Why is my enemy stopping before it reaches the player? 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