- Home /
Launching my character with a cannon.
Hello guys, i started unity just a couple of weeks ago, i want to make a cannon that launches my player to the next part of my map, i seem to be unable to do this. can anyone help me out.
(i tried to google it but nothing comes up)
Answer by Viluredfish · Mar 27, 2012 at 01:37 PM
http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html
look to this, just check if you'r Character got a RigidBody.
it can help you too : http://unity3d.com/support/documentation/ScriptReference/ForceMode.Impulse.html
Answer by reptilebeats · Mar 27, 2012 at 01:56 PM
there are a few different ways you can do this depending on what you are looking for, if say for instance you want the cannon to shoot the player to the same location every time then your best bet would be to animate it with some nice particle effects, explosion and around the character.
you could even go as far as making the background blur a bit to make it even better but theres a million different ways for the effect depending on the game, i wouldnt want stars as the explosion if im making a horror game but i would if im making a fun cartoon game.
the other way that i use for other objects in my game is simply applying force to the character, so you could apply force to the character at the cannons direction, however this isnt going to be accurate, as its real time phyisics and it depends on your character.
Answer by FlavioVolpeJr · Oct 16, 2020 at 08:19 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);
}
}
}
}
Your answer
Follow this Question
Related Questions
Launching player with cannon 2 Answers
Missile not launching. 1 Answer
Import Componet ( Variable ) Error Unexpected Token 3 Answers
FPS Animations for head and arms 0 Answers
Target frame rate between scenes 1 Answer