Why my camera block between two position using lerp? - c#

im trying to make a script that allow my camera moving into 4 differents position ( front back left right ) using keyArrows and lerp.
the first movement works good, but when i hit another KeyArrow my camera move a little bit and get stucks between the first position and the end position.
There is the code :
void Update()
{
if (Input.GetKey(KeyCode.UpArrow)){
Uparr = true;
}
if (Input.GetKey(KeyCode.DownArrow)){
DownAarr= true;
}
if (Input.GetKey(KeyCode.RightArrow)){
Rightarr = true;
}
if (Input.GetKey(KeyCode.LeftArrow)){
Leftarr = true;
}
//boolean
if(Uparr){
cam.transform.LookAt(target);
cam.transform.position = Vector3.Lerp(StartPos.position,endPosition1.position,lerpSpeed*Time.deltaTime);
if (cam.transform.position == endPosition1.position){
Uparr = false;
}
}
if(DownAarr){
cam.transform.LookAt(target);
cam.transform.position = Vector3.Lerp(StartPos.position,endPosition2.position,lerpSpeed*Time.deltaTime);
if (cam.transform.position == endPosition2.position){
DownAarr = false;
}
}
if(Rightarr){
cam.transform.LookAt(target);
cam.transform.position = Vector3.Lerp(StartPos.position,endPosition3.position,lerpSpeed*Time.deltaTime);
if (cam.transform.position == endPosition3.position){
Rightarr = false;
}
}
if (Leftarr){
cam.transform.LookAt(target);
cam.transform.position = Vector3.Lerp(StartPos.position,endPosition4.position,lerpSpeed*Time.deltaTime);
if (cam.transform.position == endPosition4.position){
Leftarr = false;
}
}
do you know what it can be the problem ?

You need to set the other direction variables to false when setting the new direction. Multiple views are active at once, and thus, they are fighting.
if (Input.GetKey(KeyCode.UpArrow)){
Leftarr = false;
Downarr = false;
Rightarr = false;
Uparr = true;
}
It may be easier to store one variable for direction but to each their own.

Related

Same trigger enter tag, but two different actions

I have troubles trying to make two different actions on trigger enter, its same name tag so I have no idea how to do it. For example I have ground button which has function, etc on first trigger enter it is disabling rotation of some other object, on second trigger enter it enables rotation back.Tried with bools but its not working well, Im thinking about int, but Im not sure. Thanks for any help, Im new in coding.
Heres the code;
if (other.gameObject.CompareTag("Player"))
{
var buttonRenderer = button.GetComponent<Renderer>();
buttonRenderer.material.SetColor("_Color", Color.red);
Rotationplane.isRotated = false;
}
EDIT:
#derHugoDont know mate, lost my mind...but finally I managed to do it over the if statements, I have no idea does it effect the performance. Heres code if someone has the same problem. Opening the bottle of whiskey...done for today.
private void FixedUpdate()
{
if(firstCollisionDone == true)
{
firstCollisionAavaliable = false;
secondCollisionAvaliable = true;
}
if(secondCollisionDone == true)
{
secondCollisionAvaliable = false;
firstCollisionAavaliable = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
if(firstCollisionAavaliable == true)
{
var buttonRenderer = button.GetComponent<Renderer>();
buttonRenderer.material.SetColor("_Color", Color.red);
Rotationplane.isRotated = false;
firstCollision = true;
}
if(secondCollisionAvaliable == true)
{
var buttonRenderer = button.GetComponent<Renderer>();
buttonRenderer.material.SetColor("_Color", Color.white);
Rotationplane.isRotated = true;
secondCollision = true;
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
if(firstCollision == true)
{
firstCollisionDone = true;
firstCollision = false;
firstCollisionAavaliable = false;
secondCollisionDone = false;
}
if(secondCollision == true)
{
secondCollisionDone = true;
secondCollision = false;
secondCollisionAvaliable = false;
firstCollisionDone = false;
}
}
}
}```
I assume the flag you want to change is the Rotationplane.isRotated.
You could simply invert it's state like e.g.
if (other.gameObject.CompareTag("Player"))
{
var buttonRenderer = button.GetComponent<Renderer>();
buttonRenderer.material.SetColor("_Color", Color.red);
Rotationplane.isRotated = !Rotationplane.isRotated;
}

Game objects scaling up when transform.parent set to null

I'm working on a small game where objects are put in a boat, then a key press makes the boat "sail".
To move all the objects that are standing on the boat, i am setting the parent of each object to an empty guide object in the boat then changing the position of the boat. (I have also tried parenting the objects into the boat object itself)
The following is a script applied to the boat object.
variables set in the BoatScript class:
public class BoatScript : MonoBehaviour {
public List<string> boatList;
public KeyCode interact;
public GameObject tempObject;
public string whichSide;
public string direction;
public bool canSail;
}
Start and Update method:
void Start () {
canSail = false;
whichSide = "start";
direction = "toFinish";
speed = 0f;
}
void Update () {
if (canSail == true)
{
SetSail();
}
if (boatList.Contains("FARMER") && whichSide == "start" && Input.GetKeyDown(interact))
{
speed = 0.5f;
CharacterCheck();
}
else if (boatList.Contains("FARMER") && whichSide == "finish" && Input.GetKeyDown(interact))
{
speed = -0.05f;
CharacterCheck();
}
}
Here are my OnTrigger methods:
void OnTriggerEnter(Collider other)
{
Debug.Log(other.gameObject.name + " collided with " + gameObject.name);
promptText.text = "";
if(CheckValidObject(other.gameObject.name) == true) {
boatList.Add(other.gameObject.name);
logBox.text = logBox.text + "\nThe " + other.gameObject.name + " is in the boat";
}
if (other.gameObject.name == "FARMER")
{
promptText2.text = "Press E to set sail";
}
}
void OnTriggerExit(Collider other)
{
boatList.Remove(other.gameObject.name);
logBox.text = logBox.text + "\nThe " + other.gameObject.name + " has left the boat";
promptText.text = "";
if (other.gameObject.name == "FARMER")
{
promptText2.text = "";
}
}
Setting sail:
void SetSail()
{
promptText.text = "";
promptText2.text = "";
addParents();
if (whichSide == "sailing" && direction == "toFinish")
{
speed = 0.05f;
gameObject.transform.Translate(speed, 0, 0);
}
else if (whichSide == "sailing" && direction == "toStart")
{
speed = -0.05f;
gameObject.transform.Translate(speed, 0, 0);
}
else if (whichSide == "start" || whichSide == "finish")
{
gameObject.transform.Translate(speed, 0, 0);
removeParents();
}
}
void addParents()
{
foreach(string o in boatList)
{
GameObject obj = GameObject.Find(o);
obj.GetComponent<Rigidbody>().useGravity = false;
obj.GetComponent<Rigidbody>().isKinematic = true;
if (obj.name == "FARMER") { obj.transform.parent = playerGuide.transform; }
else {obj.transform.parent = itemGuide.transform; }
}
}
void removeParents()
{
foreach (string o in boatList)
{
GameObject obj = GameObject.Find(o);
obj.GetComponent<Rigidbody>().useGravity = true;
if(obj.name != "FARMER") {obj.GetComponent<Rigidbody>().isKinematic = false; }
obj.transform.parent = null;
}
}
The problem: Once the boat reaches and hits the collider for the other side, the boat stops as expected but the objects that were just removed from the parent begin to scale up continuously like this:
e.g 1 https://i.gyazo.com/d35ae729757b8e71c25fd1b4a3857dae.mp4
e.g 2 https://i.gyazo.com/80637919bfd114a42d187300b7faef25.mp4
I'm not too sure what is causing this. Any help is much appreciated, thank you.
Instead of setting the parent via transform.parent, use transform.SetParent(targetTransform, false);. The second, bool, parameter determines if the game object's transform will maintain it's position, orientation, and scale. By setting it to false, the transform will maintain it's current values, while setting it to true will modify the position, orientation, and scale to maintain the world position. You can check this for further info transform.SetParent
Are youu sure it is scaling up and not moving up in the Z axes? From what im looking it is going towards the camera but not scaling up. You should debug the position and scale in the update method to see whats really happening there.
Below comment: "Well then you will have to debug it more carefully, i would first try, setting canSail to false as soon as it reaches the end. Perhaps the method addParent which is always being executed is wrong, what does the object itemGuide does? edit: i just seen the second video, from my perspective that seems gravity, what you meant with problems with scaling up is because its moving out from the boat?"
Solution:
void SetSail()
{
promptText.text = "";
promptText2.text = "";
addParents();
if (whichSide == "sailing" && direction == "toFinish")
{
speed = 0.05f;
gameObject.transform.Translate(speed, 0, 0);
}
else if (whichSide == "sailing" && direction == "toStart")
{
speed = -0.05f;
gameObject.transform.Translate(speed, 0, 0);
}
else if (whichSide == "start" || whichSide == "finish")
{
gameObject.transform.Translate(speed, 0, 0);
canSail = false; //Adding this line solves the issues, must have been affecting transform properties to child objects.
removeParents();
}
}

How can i make the checkboxes to effect also in editore mode and when the game is running?

I have 3 checkboxes
public bool stateForward = false, stateReverse = false, stateRandom = false;
I want that i will be able to chose each time only one checkbox. But also in editor mode and also when the game is running. And when the game is running i want to make it effect on the game.
In the top of the script i added:
[ExecuteInEditMode]
I tried to do in the Start function
void Start()
{
while (true)
{
if (stateForward == true)
{
stateRandom = false;
stateReverse = false;
}
else if (stateReverse == true)
{
stateRandom = false;
stateForward = false;
}
else if (stateRandom == true)
{
stateForward = false;
stateReverse = false;
}
}
anims = GetComponent<Animations>();
waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
objectsToMove = GameObject.FindGameObjectsWithTag("Robots");
originalPosition = objectsToMove[0].transform.position;
}
But i'm getting on anims: Unreachable code detected
And in the Update function:
void Update()
{
if (MyCommands.walkbetweenwaypoints == true)
{
DrawLinesInScene();
anims.PlayState(Animations.AnimatorStates.RUN);
WayPointsAI();
}
}
And in WayPointsAI
private void WayPointsAI()
{
if (stateForward == true)
{
if (targetIndex == waypoints.Length)
targetIndex = 0;
}
if (stateReverse == true)
{
if (targetIndex == 0)
targetIndex = waypoints.Length;
}
waypoint = waypoints[targetIndex].transform;
float distance = Vector3.Distance(objectsToMove[0].transform.position, waypoint.transform.position);
objectsToMove[0].transform.rotation = Quaternion.Slerp(objectsToMove[0].transform.rotation, Quaternion.LookRotation(waypoint.position - objectsToMove[0].transform.position), rotationSpeed * Time.deltaTime);
//move towards the player
if (distance < 30)
{
objectsToMove[0].transform.position += objectsToMove[0].transform.forward * slowDownSpeed * Time.deltaTime;
}
else
{
objectsToMove[0].transform.position += objectsToMove[0].transform.forward * moveSpeed * Time.deltaTime;
}
if (distance < 2)
{
if (stateForward == true)
targetIndex++;
if (stateReverse == true)
targetIndex--;
}
}
First off, you are never going to get out of your Start() method. You have a while(true) loop that you never break out of, so you will be stuck there.
Instead of multiple checkboxes that you can only choose one of, just use an enumeration. Unity will give you a drop down and you can choose one of the three available states.
enum MyStateEnum{ Forward, Reverse, Random }
public MyStateEnum State;
If you want them to be exclusive, you should probably use a single enum field instead of three bool fields. Your enum would have values Forward, Reverse, and Random, while your MonoBehavior would have a single field of your enum type. This ensures that only one will be chosen at any given time, and the editor should show a drop-down for selecting which value rather than a series of checkboxes. This not only fixes the editor UI issue, but will also lead to cleaner code with fewer potential bugs.

Detecting collision between two rectangles on a WPF Canvas

I am extremely new to programming, and I have started off with C#. I am now trying to make my first game, I decided on snake. So far I have been trying to research this question, but all of the answers I see are ones that pertain to people who are using a different method of moving their snake around.
My program uses two doubles (left and top) in order to store where the snake is on the Canvas. My program also uses two doubles for the "food" in the game, called randomFoodSpawnLeft and randomFoodSpawnTop.
My question is this. How does one detect collision between two rectangular objects with just a left and top value? I am rather confused.
snakeWindow is the Window, snakeHead is the rectangle that represents a snake, left is the left value of the snake, top is the top value of the snake.
void timer_Tick(object sender, EventArgs e)
{
double left = Canvas.GetLeft(snakeHead);
double top = Canvas.GetTop(snakeHead);
if (keyUp)
{
top -= 3;
}
else if (keyDown)
{
top += 3;
}
else if (keyLeft)
{
left -= 3;
}
else if (keyRight)
{
left += 3;
}
// These statements see if you have hit the border of the window, default is 1024x765
if (left < 0)
{
left = 0;
gameOver = true;
}
if (top < 0)
{
top = 0;
gameOver = true;
}
if (left > snakeWindow.Width)
{
left = 0;
gameOver = true;
}
if (top > snakeWindow.Height)
{
top = 0;
gameOver = true;
}
// Statements that detect hit collision between the snakeHead and food
//
if (foodEaten == true)
{
spawnFood();
textBlockCurrentScore.Text += 1;
}
// If gameOver were to be true, then the game would have to end. In order to accomplish this I display to the user that the game is over
// and the snakeHead is disabled, and should restart.
if (gameOver == true)
{
keyRight = false;
keyLeft = false;
keyUp = false;
keyDown = false;
top = 0;
left = 0;
textBlockGameOver.Text = "GAME OVER!";
snakeCanvas.Background = Brushes.Blue;
}
Canvas.SetLeft(snakeHead, left);
Canvas.SetTop(snakeHead, top);
}
You can use System.Windows.Rect.IntersectsWith. Try it like this:
Rect rect1 = new Rect(left1, top1, widht1, height1);
Rect rect2 = new Rect(left2, top2, widht2, height2);
bool intersects = rect1.IntersectsWith(rect2);
Of course you will have to check the snake's head against all it's parts.

Dart Scoring Game Using C# Visual Studio 2010 - Alternating player score after evey 3rd score

Just bought a dartboard and thought to create a little scoring application, so far i have set up the player names and individual buttons to start the game. three games - 301, 501 and 1001, these are the targets scores to get to zero by throwing three darts each in turn by two players.
I have included many buttons for the score of each dart from 20 down to 1, each for single, double, triple, bull, outer bull and a no score button. Once the games starts the first three button presses should be attributed to player one then the following three buttons pressed to allocate the corresponding score to player 2. The games ends with the winning player achieving the target score (or whittles it down to zero).
I could have a player selection button to do this but was after some tips on a way of coding the alternate pattern of scoring to be automatic.
Any help greatly appreciated. Thank you
namespace dbstats
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
btnPlay.Visible = false;
btnReset.Visible = false;
btnUndo.Visible = false;
gbDartBoard.Visible = false;
gbScoreBoard.Visible = false;
lbP1Select.Visible = false;;
lbP2Select.Visible = false;
cmbP1.Visible = false;
cmbP2.Visible = false;
string[] lines = File.ReadAllLines(#"playerStats.csv");
foreach (var line in lines)
{
string[] names = line.Split(',');
if (names[0] != "NAME")
{
cmbP1.Items.Add(names[0]);
cmbP2.Items.Add(names[0]);
}
}
}
private void tsm301_Click(object sender, EventArgs e)
{
lbP1Select.Visible = true;
cmbP1.Visible = true;
lbP2Select.Visible = true;
cmbP2.Visible = true;
btnPlay.Visible = true;
tbPlayer1.Text = "301";
tbPlayer2.Text = "301";
gamesToolStripMenuItem.Visible = false;
manageToolStripMenuItem.Visible = false;
}
private void btnPlay_Click(object sender, EventArgs e)
{
if (cmbP1.SelectedItem == cmbP2.SelectedItem || cmbP1.SelectedItem == null || cmbP2.SelectedItem == null)
{
MessageBox.Show("Make Sure:" + "\n\n"
+ "The Players are NOT the same." + "\n"
+ "&" + "\n"
+ "At least one selection is NOT left blank", "Choose Again!");
}
else
{
lbP1Select.Visible = false;
cmbP1.Visible = false;
lbP2Select.Visible = false;
cmbP2.Visible = false;
btnPlay.Visible = false;
lbPlayer1.Text = cmbP1.SelectedItem.ToString();
lbPlayer2.Text = cmbP2.SelectedItem.ToString();
btnReset.Visible = true;
btnUndo.Visible = true;
gbDartBoard.Visible = true;
gbScoreBoard.Visible = true;
lbPlayer1.Visible = true; ;
lbPlayer2.Visible = true;
}
}
}
Keep a turnsEntered member variable to whatever class is managing turns/scoring.
// Call after every time a new score is entered. Start at 0.
turnsEntered++;
if (turnsEntered % 3 == 0)
{
SwitchPlayer(); // However you keep track of current player - switch here
}
After the 3rd score is entered it will switch players...after 3 more it will switch again, etc...
You can later check turnsEntered to determine when the game is over. Set it back to zero when you reinitialize for a new game.

Categories