- Home /
Found a work around to my issue.
How to move a Collider in a Game Object in a Direction
Hello I wanted to know if it was possible to move a collider that's in a game object a certain direction. In this case up and then come back down. This is to make up for the animation I have made for a character that has a jumping animation and it never crossed my mind that the collision would stay the same place making avoiding things with jump pointless. I tried doing a getcomponent and sendmessageupwards command but I couldn't figure out the proper way to do it. If I could get some adive or help that would be amazing.
Here is the code and script I intend on implementing in:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float movementSpeed = 10.0f;
public float angleChangeSpeed = 100.0f;
public bool isDead = false;
private float minX = -15.0f;
private float maxX = 15.0f;
private float minZ = 0.0f;
private float maxZ = 20.0f;
// Update is called once per frame
void Update ()
{
if(!isDead)
{
transform.localPosition = new Vector3(Mathf.Clamp(transform.localPosition.x, minX, maxX), 0,
Mathf.Clamp(transform.localPosition.z, minZ, maxZ));
// new code
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
Vector3 direction = new Vector3 (horizontal, 0, vertical);
Vector3 finalDirection = new Vector3 (horizontal, 0, 1.0f);
transform.position += direction * movementSpeed * Time.deltaTime;
transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (finalDirection), Mathf.Deg2Rad*angleChangeSpeed);
}
if (!isDead)
{
if (Input.GetButton ("Jump"))
{
//////////////RIGHT HERE////////////////////////////////////
}
}
}
void SetDead(bool dead)
{
Debug.Log ("SetDead was reached");
isDead = dead;
}
}
Answer by danielmetlitski · Jan 21, 2015 at 08:37 AM
this.collider.transform.Translate(Vector3.down* Time.deltaTime * 5);
should work.
Thanks for the reply , but from the code you have provided me is giving me weird error messages that says Time.deltaTime
of Vector3.down Time.deltaTime 5
is the cause of it. $$anonymous$$aybe its a syntax error, I'll try with the catalog(or whatever it's called) related to Time.deltaTime on the mean time. I really want to fix this something so small but game breaking.
Okay I tried one more attempt in fixing this but I am getting nothing this is $$anonymous$$d boggling. I even checked it here link text but I am not getting anything. If someone can also suggest another way of doing this please do.
for some reason it doesnt show up on my answer, you multiply Vector3.down by Time.deltaTime by 5
this.collider.transform.Translate(Vector3.down times Time.deltaTime times 5);
the star thing does show up for some fing reason on my answers *
Thank you for trying to help me but I've found a work around that works just as fine. This wasn't meant to be a realistic game for me to create anyways. So thank you again I'll be closing this now.