///Question : How to change the camera to the other player without it being buggy
So I have a piece of code which is changing the variable selecterChar from 0 to 1 or reverse every time the user presses 'E'and it works great. This is in a different Script which I access in this one with this code
private void ChangeCharacter()
{
GameObject thePlayer = GameObject.Find("Walk (1)");
PlayerMovement playerMovemnt = thePlayer.GetComponent<PlayerMovement>();
int selectedCharacter = playerMovemnt.selectedChar;
selCharacter = selectedCharacter;
}
Now the other part of this script is used for the camera. I want to change the camera with this
private void Start()
{
offset = transform.position - player.transform.position;
offset = transform.position - player2.transform.position;
}
private void LateUpdate()
{
if (selCharacter == 0)
{
transform.position = player.transform.position + offset;
}
else
{
transform.position = player2.transform.position + offset;
}
}
So if the selCharacter (declared in the start of this script) is 0 the camera moves to player but if it is 1 then it goes to player2. So the ChangeCharacter method is used only 1 time when the game starts but if I move the code from ChangeCharacter() to lateUpdate() it works but is changing the camera multiple times before it stops and if it is in the ChangeCharacter() method and I call it from the LateUpdate() one it works only for the first player.
Changed Input.GetKey(KeyCode.E)to Input.GetKeyDown(KeyCode.E)and then added this
GameObject thePlayer = GameObject.Find("Walk (1)");
PlayerMovement playerMovemnt = thePlayer.GetComponent<PlayerMovement>();
int selectedCharacter = playerMovemnt.selectedChar;
selCharacter = selectedCharacter;
to the Update method as i did not need the other one and it worked.
PS : Will check it as answered tomorrow.
Related
I am making my first foray into AI, specifically AI that will follow the player.
I am using the A* Path finding project scripts, but used the Brackeys tutorial for the code
https://www.youtube.com/watch?v=jvtFUfJ6CP8
Source in case needed.
Here's the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
public class EnemyAI : MonoBehaviour
{
public Transform Target;
public float speed = 200f;
public float NextWayPointDistance = 3f;
Path path;
int currentWaypoint = 0;
bool ReachedEndOfpath = false;
Seeker seeker;
Rigidbody2D rb;
public Transform EnemyGraphics;
// Start is called before the first frame update
void Start()
{
seeker = GetComponent<Seeker>();
rb = GetComponent<Rigidbody2D>();
InvokeRepeating("UpdatePath", 0f, 1f);
}
void UpdatePath()
{
if (seeker.IsDone())
seeker.StartPath(rb.position, Target.position, OnPathComplete);
}
void OnPathComplete(Path p)
{
if (!p.error)
{
path = p;
currentWaypoint = 0;
}
}
// Update is called once per frame
void fixedUpdate()
{
if(path == null)
return;
if(currentWaypoint >= path.vectorPath.Count)
{
ReachedEndOfpath = true;
return;
}
else
{
ReachedEndOfpath = false;
}
Vector2 Direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
Vector2 Force = Direction * speed * Time.fixedDeltaTime;
rb.AddForce(Force);
float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);
if(distance < NextWayPointDistance)
{
currentWaypoint++;
}
if(rb.velocity.x >= 0.01f)
{
EnemyGraphics.localScale = new Vector3(-1f, 1f, 1f);
}else if(rb.velocity.x <= 0.01f)
{
EnemyGraphics.localScale = new Vector3(1f, 1f, 1f);
}
}
}
How I tried to fix the issue:
I thought it could've been a problem with the speed so I increased it to 10000000, and still nothing
Next I thought it was a problem with the Rigidbody2d so I check there, found the gravity scale was set at 0, so I increased it to 1. It made my enemy fall to the ground but still no movement.
I thought it could've been a problem with the mass and drag, so I set Linear drag and Angular drag to 0, and also set mass to 1. Still nothing.
I set the body type to kinematic, pressed run, nothing. Set the body type to static, pressed run, nothing. Set the body type to Dynamic, pressed run, still nothing.
I tried to make a new target for the enemy to follow, dragged the empty game object i nto the target, pressed run and still didn't move.
I am at a loss on how to fix this.
Please help?
Looks like maybe a typo? You have:
// Update is called once per frame
void fixedUpdate()
{
but the method is called FixedUpdate(), with a big F in front. You have fixedUpdate, which is NOT the same.
I've been trying various fixes for days now and I cannot figure this out. I need a bullet to spawn and move until collision, but I cannot even get it to move (it spawns just fine). Note: This is a 2D game.
Here is my code:
public class Enemy : MonoBehaviour
{
public float timeBetweenFire = 2;
public Rigidbody2D bullet;
public Transform gunSpawn;
private float timer;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update() {
timer+=Time.deltaTime;
if (timer >= timeBetweenFire) {
Shoot();
timer = 0;
}
}
private void Shoot() {
Rigidbody2D bulletInstance;
bulletInstance = Instantiate(bullet, gunSpawn.position, gunSpawn.rotation) as Rigidbody2D;
//bulletInstance.AddForce(gunSpawn.forward * 1000f);
//bulletInstance.AddForce(Vector3.up * 10 * Time.deltaTime);
//bulletInstance.AddForce(transform.forward * 100);
bulletInstance.AddForce(Vector3.up * 1000);
}
}
The commented bulletInstance methods are what I've recently tried, I did a bunch of large scale code changes regarding bullet (iirc changed it around between Rigidbody, GameObject, and Transform) and nothing helped. The bullet spawns a good bit away from anything except the gunSpawn, which is just an empty GameObject, so I don't think collisions are an issue. I'm a new Unity programmer so forgive me for any dumb mistakes.
UPDATE:
This is what I did to get it (mostly) working. When the bullet collides it can spin out, but at least I made some progress.
public class Deflect : MonoBehaviour
{
private Rigidbody2D bullet;
public float bulletSpeed = 0.1f;
Vector2 direction = new Vector3(0.7f,0.7f);
ContactPoint2D[] myContact = new ContactPoint2D[1];
Vector2 _velocity;
public void Update ()
{
}
// Start is called before the first frame update
void Start()
{
bullet = this.GetComponent<Rigidbody2D>();
_velocity = bullet.velocity;
}
void OnCollisionEnter2D(Collision2D collision)
{
foreach (ContactPoint2D contact in collision.contacts)
{
print(contact.collider.name + " hit " + contact.otherCollider.name);
// Visualize the contact point
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
Vector2 inNormal = collision.contacts[0].normal;
_velocity = Vector3.Reflect(_velocity, inNormal);
bullet.velocity = _velocity;
}
}
1 - Create your bullet prefab as a GameObject
2 - Add a RigidBody2D to it
3 - Make sure the isKinematic is unchecked on the Rigibody2d
4 - then
GameObject bulletInstance = Instantiate(bullet, gunSpawn.position,gunSpawn.rotation);
bulletInstance.GetComponent<Rigidbody2D>().AddForce(Vector3.up * 1000);
IsKinematic prevents the Rigidbody2D to be affected by forces .
https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html
Make sure that the bullet has a rigid-body component and that it is not set to kinematic.
So, this code just isn't responding for whatever reason. I have an enemy that I'm trying to get to face the player at all times (The enemy swoops over the player's head back and forth periodically). But other than making a flip happen whenever a timer hits 0, which doesn't really work all that well, I can't get the Flip function to work. I know the Flipper function is fine; I already tested it out and everything. I'm just not sure how to tell the enemy that when the player is to the left of it, to turn, and vice versa.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dragoonDetection : MonoBehaviour {
private Rigidbody2D rb;
private Animator anim;
public Transform Player;
private bool facingRight = true;
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
}
void Update()
{
Flip();
}
void Flip()
{
if (Player.transform.localScale.x > 0) {
transform.localScale = new Vector3 (1.69f, 1.54f, 1f);
}
if (Player.transform.localScale.x < 0) {
transform.localScale = new Vector3 (-1.69f, 1.54f, 1f);
}
}
void Flipper()
{
facingRight = !facingRight;
Vector2 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Got any ideas? I'd rather avoid using FindGameObject because it's not actually looking for the player script. It's looking for a child transform with no script attached to the player. And because I have two different player GameObjects that you can switch to anytime in the game, it wouldn't really work for me in that regard.
You will need to perform a check of some sort against the players position with the bird position if you want it to face the player at all times. A barebones method would just be to compare the x-positions of the two objects and change the scale accordingly.
void Update()
{
transform.localScale = new Vector3(getDir()*1.69f, 1.54f, 1);
}
private int getDir()
{
if (player.transform.position.x < transform.position.x)
return -1;
else
return 1;
}
You should throw some additional checks in here to keep it from updating the scale every frame when there is no change.
I'm making a game in which the player controls two different characters (each one has its own empty object with a camera as child), and switchs one or another by pressing the control key. The thing is, I'm trying to make a little transition between both characters cameras by using another camera, so it doesn't just teleports between one and another but I can't seem to do it. I tried with lerp but I don't know if I got it right, so I read and tried Vector3.MoveTowards but still couldn't do it. This is my code so far (the while is because a last-moment-braindead I had):
public class CameraController : MonoBehaviour
{
public Camera cam1;
public Camera cam2;
public Camera movingCamera;
public bool isCurrentPlayer;
public Transform target1;
public Transform target2;
public float speed = 0.2f;
void FixedUpdate()
{
float step = speed * Time.deltaTime;
if (Input.GetButtonDown("Control"))
{
if (isCurrentPlayer)
{
movingCamera.enabled = true;
cam2.enabled = false;
while (transform.position != target1.position)
{
transform.position = Vector3.MoveTowards(transform.position, target1.position, step);
}
if (transform.position == target1.transform.position)
{
movingCamera.enabled = false;
cam1.enabled = true;
}
isCurrentPlayer = false;
}
else if (!isCurrentPlayer)
{
movingCamera.enabled = true;
cam1.enabled = false;
while (transform.position != target2.position)
{
transform.position = Vector3.MoveTowards(transform.position, target2.position, step);
}
if (transform.position == target2.transform.position)
{
movingCamera.enabled = false;
cam2.enabled = true;
}
isCurrentPlayer = true;
}
}
}
I'm curious about two things. Why did you use FixedUpdate to manage your updates? This isn't physics code. Is there a particular reason you are using multiple cameras? If I may, I propose the following changes.
You can simply make use of the main camera instead of multiple cameras. Additionally, you can increase the number of player objects you can toggle through by using an array of player GameObjects, and by changing the input parameters to left control and right control, you can toggle between next player and previous player to navigate bi-directionally through the array of players.
Here's my example code that implements these changes (tested and works, though improvements can be made.)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Attached to Main Camera
public class CameraController : MonoBehaviour {
// set manually in inspector
public GameObject[] players;
public float movementSpeed = 1.0f;
public float rotationSpeed = 1.0f;
private int currentPlayer;
private float startTime;
private float distanceToPlayer;
private Vector3 startPosition;
private Quaternion startOrientation;
// Use this for initialization
void Start () {
currentPlayer = 0;
ResetCamera();
}
// Update is called once per frame
void Update () {
float distanceCovered;
float rotationCovered;
float fractionTraveled;
// switch to previous
if (Input.GetButtonDown("left ctrl")) {
if (currentPlayer == 0) currentPlayer = players.Length - 1;
else currentPlayer--;
ResetCamera();
}
// switch to nextPlayer
if (Input.GetButtonDown("right ctrl")) {
if (currentPlayer == players.Length - 1) currentPlayer = 0;
else currentPlayer++;
ResetCamera();
}
// Keep moving camera
if (transform.position != players[currentPlayer].transform.position)
{
distanceCovered = (Time.time - startTime) * movementSpeed;
fractionTraveled = distanceCovered / distanceToPlayer;
rotationCovered = (Time.time - startTime) * rotationSpeed;
// Lerp to player position
transform.position = Vector3.Lerp(
startPosition,
players[currentPlayer].transform.position,
fractionTraveled
);
// match player orientation
transform.rotation = Quaternion.RotateTowards(
transform.rotation,
players[currentPlayer].transform.rotation,
rotationCovered
);
// Stop moving camera
} else {
// Match orientation
if (transform.rotation != players[currentPlayer].transform.rotation)
transform.rotation = players[currentPlayer].transform.rotation;
// Set parent transform to current player
transform.parent = players[currentPlayer].transform;
}
}
void ResetCamera() {
transform.parent = null;
startTime = Time.time;
startPosition = transform.position;
startOrientation = transform.rotation;
distanceToPlayer = Vector3.Distance(
transform.position,
players[currentPlayer].transform.position
);
}
}
Obviously the values would need to be tweaked, and the movement algorithm is pretty basic. Crude but function. You can also add player movement code into the camera, just make sure it points to players[currentPlayer] and it will work for each of your player objects without having to use additional scripts unless there is a reason to do so.
Feel free to use this code. Like I said, it works. However, should you choose to do so, it can easily be modified to function like your original code simply by removing the array and reinstating the individual GameObjects.
transform.position points to the position of CameraController game object. If you want to move movingCamera you probably want to use movingCamera.transform.position. Also keep in mind that in your script the MoveTowards() would only fire when you are pressing your "Control" button.
As memBrain said it would be the best practice to use only one camera for this - visually it would look the same.
The script should look something like this:
// Assuming target1 is player 1 and target2 is player 2
private float snapThreshold = 0.1f;
private Vector3 movingCameraDestination = Vector3.zero;
void FixedUpdate()
{
if(Input.GetButtonDown("Control"))
{
if(isCurrentPlayer)
{
//Set position of transition camera to player 1 and set it's destination to player's 2 position
movingCamera.transform.position = player1.position;
movingCameraDestination = player2.position;
//Disable player 1 camera and enable transition camera
cam1.enabled = false;
movingCamera.enabled = true;
}
else
{
//Set position of transition camera to player 21 and set it's destination to player's 1 position
movingCamera.transform.position = player2.position;
movingCameraDestination = player1.position;
//Disable player 1 camera and enable transition camera
cam2.enabled = false;
movingCamera.enabled = true;
}
}
//If transition camera is enabled and its destination is not Vector3.zero - move it
if(movingCameraDestination != Vector3.zero && movingCamera.enabled)
{
movingCamera.transform.position = Vector3.Lerp(movingCamera.transform.position, movingCameraDestination, speed * Time.deltaTime);
//If the distance between transition camera and it's destination is smaller or equal to threshold - snap it to destination position
if(Vector3.Distance(movingCamera.transform.position, movingCameraDestination) <= snapThreshold)
{
movingCamera.transform.position = movingCameraDestination;
}
//If transition camera reached it's destination set it's destination to Vector3.zero and disable it
if(movingCamera.transform.position == movingCameraDestination)
{
movingCameraDestination = Vector3.zero;
movingCamera.enabled = false;
}
}
}
first of all thank you all for reading my question,
i am trying to create a for ever running game like subway surface, i started the update() function in the payerControl script py forcing the rigid body to move forward with the speed of 10 as mintiond in the code then i added the jumping input when you press the upArrow on the keyboard , but the problem here is that the character sometimes jump and most of the time dose not jump, because the jump boolean become true for less than one second and then go back to false , so is their any way to make the jump process to stay for 2 seconds ?
Character control code :
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
public Animator anim;
public Rigidbody rbody;
public float verticalJumpPower;
public float horizantalJumpPower;
public float playerVelocity;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
rbody = GetComponent<Rigidbody>();
verticalJumpPower = 50f;
horizantalJumpPower = 30f;
playerVelocity = 10f;
}
// Update is called once per frame
void Update ()
{
rbody.velocity = new Vector3(rbody.velocity.x,rbody.velocity.y,playerVelocity*Time.deltaTime);
rbody.transform.rotation = Quaternion.identity;
if(Input.GetKeyDown (KeyCode.UpArrow))
{
anim.SetBool("jump",true);
rbody.AddForce(new Vector3(0,verticalJumpPower,horizantalJumpPower));
}
else
{
anim.SetBool("jump", false);
}
if(Input.GetKey(KeyCode.LeftArrow))
{
Vector3 currentPosition = rbody.transform.position;
Vector3 leftPosition = new Vector3(rbody.transform.position.x+Constants.PLAYER_LEFT_DISTANCE, rbody.transform.position.y,rbody.transform.position.z);
rbody.transform.position = Vector3.Lerp(currentPosition,leftPosition,Time.deltaTime);
}
if(Input.GetKey(KeyCode.RightArrow))
{
Vector3 currentPosition = rbody.transform.position;
Vector3 rightPosition = new Vector3(rbody.transform.position.x+Constants.PLAYER_RIGHT_DISTANCE, rbody.transform.position.y,rbody.transform.position.z);
rbody.transform.position = Vector3.Lerp(currentPosition,rightPosition,Time.deltaTime);
}
if(Input.GetKey(KeyCode.DownArrow))
{
anim.SetBool("isSlide",true);
rbody.velocity = new Vector3(0f, 0f,5f);
anim.applyRootMotion = false;
}
else
{
anim.SetBool("isSlide", false);
anim.applyRootMotion = true;
}
}
}
GetKeyDown() returns true only in the frame of the initial keypress. Right now what I expect to happen is this:
One the first frame of keydown it triggers the jump animation and adds the force, the next frame this line
rbody.velocity = new Vector3(rbody.velocity.x,rbody.velocity.y,playerVelocity*Time.deltaTime);
resets the z velocity then we hit the else condition
else
{
anim.SetBool("jump", false);
}
and turn off the animation. You might want to make the jump animation run for 2 seconds and have it trigger an event to reset the velocity afterwards. Or you will want to instead of having the effect run immediately, have the keypress change some state of the object (perhaps store a game time after which you should clear the flag and reset the velocity and animation state)