C
C#7mo ago
NerdyPegasus

Cannon not following mouse?

Hello! I'm trying to have my cannon change position based on where the mouse is, but it always chooses to the same angle when the mouse is clicked no matter its placement on the screen. Does anyone know what is causing this? And what I need to fix and with what? C# in Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CannonManager : MonoBehaviour
{
public GameObject cannonBallPrefab;
public Transform firePoint;


private Camera _cam;
private bool _pressingMouse = false;

private Vector3 _initialVelocity;

void Start()
{
_cam = Camera.main;
}

void Update()
{
if (Input.GetMouseButtonDown(0))
_pressingMouse = true;
if (Input.GetMouseButtonUp(0))
{
_pressingMouse = false;
_Fire();
}

if (_pressingMouse)
{
// coordinate transform screen > world
Vector3 mousePos = _cam.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;

// look at
transform.LookAt(mousePos);
}
}

private void _Fire()
{
// instantiate a cannon ball
GameObject cannonBall = Instantiate(cannonBallPrefab, firePoint.position, Quaternion.identity);
// apply some force
Rigidbody rb = cannonBall.GetComponent<Rigidbody>();
rb.AddForce(_initialVelocity, ForceMode.Impulse);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CannonManager : MonoBehaviour
{
public GameObject cannonBallPrefab;
public Transform firePoint;


private Camera _cam;
private bool _pressingMouse = false;

private Vector3 _initialVelocity;

void Start()
{
_cam = Camera.main;
}

void Update()
{
if (Input.GetMouseButtonDown(0))
_pressingMouse = true;
if (Input.GetMouseButtonUp(0))
{
_pressingMouse = false;
_Fire();
}

if (_pressingMouse)
{
// coordinate transform screen > world
Vector3 mousePos = _cam.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;

// look at
transform.LookAt(mousePos);
}
}

private void _Fire()
{
// instantiate a cannon ball
GameObject cannonBall = Instantiate(cannonBallPrefab, firePoint.position, Quaternion.identity);
// apply some force
Rigidbody rb = cannonBall.GetComponent<Rigidbody>();
rb.AddForce(_initialVelocity, ForceMode.Impulse);
}
}
5 Replies
Buddy
Buddy7mo ago
Just quick coding convention. C# does not use _ except for backing fields. so _Fire() would be Fire() Also what perspective is the camera? First-Person? Or top-down?
NerdyPegasus
NerdyPegasusOP7mo ago
Sideways? I'm trying to make the game angry birds styled in a way
No description
NerdyPegasus
NerdyPegasusOP7mo ago
the cannonballs fire out of the cannon when the mouse is unclicked after being held down, so the fire function works, its just the angle of the cannon never changing I just realized I believe I was having issues because of my projection settings on my main camera being incorrect which was throwing off everything else Thank you for the help, I really appreciate it!
Buddy
Buddy7mo ago
:blobthumbsup: But I barely if ever helped, either way. No problem!
MODiX
MODiX7mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server