- Home /
Question by
RushedThru · Jun 05, 2015 at 04:14 PM ·
2d gamejumpraycasting
how to make gameobject jump onto other gameobject using Linecast
Hi so what im trying to make is a tree falling over across a gap and the character is always moving forward. Once he hits the tree, i want him to jump once onto the tree and continue across.
Here's a sample of my code. I am fairly new to Unity and to coding, so any tips would be much appreciated.
using UnityEngine; using System.Collections;
public class CharacterControl : MonoBehaviour {
Animator anim;
public float speed;
public Transform sightStart, sightEnd, groundedEnd;
public bool treeHit = false;
public float thrust;
public Rigidbody2D walkForce;
public bool grounded = false;
RaycastHit2D treeJump;
bool jumped;
// Use this for initialization
void Start () {
walkForce = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
walkForce.AddForce(2*Vector2.right);
Raycasting();
}
void Raycasting()
{
Debug.DrawLine(sightStart.position, sightEnd.position, Color.green);
Debug.DrawLine(this.transform.position, groundedEnd.position, Color.green);
if (Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Tree"))) {
treeHit = true;
walkForce.AddForce (Vector2.up * 10f);
}
if (treeHit = true) {
treeHit = false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
2D Character Jumps only 1 time 2 Answers
[Solved] Having trouble with 2D Character Jump using Bolt 0 Answers
2D moblie game raycast problem 1 Answer