Question by
dragonking300 · Dec 27, 2016 at 01:27 AM ·
c#scripting problemscripting beginner
My player character is jumping infinitely and it didn't use to do that...
I think when I added my cannon script my character started to jump infinity(as in just flying into the air without player input) and I noticed when ever my character's OnGround bool = false the character starts jumping. Fixes?
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // NEW
public class Jump : MonoBehaviour
{
public bool OnGround;
private Rigidbody rb;
public float jumpspeed = 10;
// NEW
private List<string> tagsToCompare = new List<string>()
{
"Ground",
"tag2",
"tag3" // Note how the last one doesn't need a comma.
};
void Start()
{
OnGround = true;
Debug.Log("OnGround set to true in Start");
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (OnGround)
{
if (Input.GetButton("Jump"))
{
rb.AddForce (new Vector3(0.0f, jumpspeed, 0.0f), ForceMode.Impulse);
OnGround = false;
Debug.Log("OnGround set to false in Update");
}
else
{
}
}
}
void OnCollisionEnter(Collision other)
{
Debug.Log("Collision entered with tag named: " + other.gameObject.tag);
if (isTargetTag(other.gameObject.tag)) // NEW
{
OnGround = true;
Debug.Log("OnGround Set To true in OnCollisionEnter, collided with tag: " + other.gameObject.tag);
}
else
{
OnGround = false;
Debug.Log("OnGround Set To false in OnCollisionEnter, collided with tag: " + other.gameObject.tag);
}
}
// NEW
private bool isTargetTag(string tagToCheck)
{
return tagsToCompare.Contains(tagToCheck);
}
}
my cannon script
using UnityEngine;
using System.Collections;
public class CannonScript : MonoBehaviour
{
float previousTime;
public GameObject
projectile, // The cannonball
target, // The cube that the cannon points at
cannonBallPos; // The spawn position of the cannon ball
public float
waitTime = 1, // Time between shots
forceAmount = 1000;
void Start()
{
previousTime = Time.timeSinceLevelLoad;
transform.localScale = new Vector3(1, 1, 2);
cannonBallPos = new GameObject("cannonBallPos"); // Creates a new game object
cannonBallPos.transform.parent = transform; // Sets the parent as the cannon
cannonBallPos.transform.position = transform.position + new Vector3(0, 0, 0.5f); // Moves to the front of the cannon
}
void Update()
{
transform.LookAt(target.transform);
if (Time.timeSinceLevelLoad - previousTime > waitTime)
{
previousTime = Time.timeSinceLevelLoad;
Fire();
}
}
void Fire()
{
GameObject cannonBall;
cannonBall = (GameObject)Instantiate(projectile, cannonBallPos.transform.position, cannonBallPos.transform.rotation);
cannonBall.GetComponent<Rigidbody>().AddForce(transform.forward * forceAmount);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612103506im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a thrown object land on a certain point e.g a thrown spear landing on its tip 0 Answers
HELP! How to make Update function start after delay? C# 2 Answers
Having trouble deleting objects from a list after they reach a certain scale. 0 Answers
Problems with respawning using a very simple script 1 Answer
Rotate Player 90 degrees about its Y axis relative to the mouse being dragged between two angles 1 Answer