Hello good day, I would like to know how to fix it, thanks!
The video
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
public Transform spawnPoint; // The point from which it is fired
public GameObject bullet; // Prefab of the bullet (now called SphereBullet)
public float shotForce = 1500f; // Force applied to the bullet
public float bulletLifeTime = 2f; // Duration of the bullet's life (range)
public float bulletSpeedMultiplier = 1.5f; // Multiplier to increase speed
// public float shorrate = 0.5f; //tutorial code
// public float shotratetime = 0; //tutorial code
void Update()
{
// Detect mouse click (or fire button) when pressed
if (Input.GetButtonDown("Fire1"))
{
if ((GameManager.Instance.gunAmmo > 0))
{
//if (Time.time > shotratetime && GameManager.Instance.gunAmmo > 0)
//{ GameManager.Instance.gunAmmo -- ;
// GameObject newbullet; cap 13 19:08!!
//}
GameManager.Instance.gunAmmo--;
// Instantiate the bullet (SphereBullet)
GameObject newBullet = Instantiate(bullet, spawnPoint.position, spawnPoint.rotation);
// Add force to the bullet to make it move
Rigidbody rb = newBullet.GetComponent<Rigidbody>();
if (rb != null)
{
// Multiply the speed of the bullet
rb.AddForce(spawnPoint.forward * shotForce * bulletSpeedMultiplier);
}
// Destroy the bullet after a certain time to limit its range
Destroy(newBullet, bulletLifeTime);
}
}
}
}
There’s no apparent camera code in your post.
How to report your problem productively in the Unity3D forums:
http://plbm.com/?p=220
This is the bare minimum of information to report:
- what you want
- what you tried
- what you expected to happen
- what actually happened, log output, variable values, and especially any errors you see
- links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?
Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.
There’s even a dedicated Camera / Cinemachine area: see left panel.
If you insist on making your own camera controller, do not fiddle with camera rotation.
The simplest way to do it is to think in terms of two Vector3 points in space:
- where the camera is LOCATED
- what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;
void LateUpdate()
{
cam.transform.position = WhereMyCameraIsLocated;
cam.transform.LookAt( WhatMyCameraIsLookingAt);
}
Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.
2 Likes
This is appearing in the 2D product area because for some reason you added a “2D-Animation” tag. Your post clearly isn’t about animation and it isn’t 2D.
Please only use the tags that relate to your question so it doesn’t appear in the wrong area.
I’ll remove that tag for you.
Thanks.