- Home /
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)
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.
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);
}
}
}
}